[Haskell-beginners] How can I replace content of tag in xml file with haxml.

Dontdie YCH dontdieych at gmail.com
Wed Sep 17 09:17:00 UTC 2014


I'm trying to replace `CurrentURI` tag's contents using HaXml.

from,

```
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <s:Body>
    <u:SetAVTransportURI xmlns:u="urn:schemas-upnp-org:service:AVTransport:1">
      <InstanceID>0</InstanceID>
      <CurrentURI>AAAAAAAAAAAA</CurrentURI>
      <CurrentURIMetaData/>
    </u:SetAVTransportURI>
  </s:Body>
</s:Envelope>
```

to,

```
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <s:Body>
    <u:SetAVTransportURI xmlns:u="urn:schemas-upnp-org:service:AVTransport:1">
      <InstanceID>0</InstanceID>
      <CurrentURI>BBBBBBBBBBBBBB</CurrentURI>
      <CurrentURIMetaData/>
    </u:SetAVTransportURI>
  </s:Body>
</s:Envelope>
```

I had look around, hxt, xml-light, and haxml. I found `onContents`
function from `Text.XML.HaXml.Wrappers` module and It looks like way
to go.

```
 λ: xml <- readFile "SetAVTransportURI.xml" :: IO String
 λ: let doc = xmlParse "dumb" xml
 λ: doc
Document (Prolog (Just (XMLDecl "1.0" Nothing Nothing)) [] Nothing [])
[] (Elem (N "s:Envelope") [(N
"xmlns:s",http://schemas.xmlsoap.org/soap/envelope/),(N
"s:encodingStyle",http://schemas.xmlsoap.org/soap/encoding/)] [CString
False "\n  " file dumb  at line 2 col 125,CElem (Elem (N "s:Body") []
[CString False "\n    " file dumb  at line 3 col 11,CElem (Elem (N
"u:SetAVTransportURI") [(N
"xmlns:u",urn:schemas-upnp-org:service:AVTransport:1)] [CString False
"\n      " file dumb  at line 4 col 79,CElem (Elem (N "InstanceID") []
[CString False "0\n      " file dumb  at line 5 col 19]) file dumb  at
line 5 col 7,CString False "\n      " file dumb  at line 6 col
20,CElem (Elem (N "CurrentURI") [] [CString False "\n" file dumb  at
line 7 col 19,CMisc (Comment " #EXTM3U ") file dumb  at line 8 col
1,CMisc (Comment " #EXTINF:5850,\52628\49437\53945\49440\50689\54868
\47609\47329\44284\44053 140909 HDTV x264 720p AC3 \50864\47532\47568
\45433\51020 - nature boy ") file dumb  at line 9 col 1,CMisc
(Comment " http://192.168.219.2:8200/MediaItems/587.mkv ") file dumb
at line 10 col 1,CMisc (Comment " #EXTM3U ") file dumb  at line 11 col
1,CMisc (Comment "
#EXTINF:6411,The.Adventures.of.Tintin.2011.1080p.BluRay.x264.YIFY ")
file dumb  at line 12 col 1,CString False
"http://192.168.219.2:8200/MediaItems/165.mp4\n      " file dumb  at
line 13 col 1]) file dumb  at line 7 col 7,CString False "\n      "
file dumb  at line 14 col 20,CElem (Elem (N "CurrentURIMetaData") []
[]) file dumb  at line 15 col 7,CString False "\n    " file dumb  at
line 15 col 28]) file dumb  at line 4 col 5,CString False "\n  " file
dumb  at line 16 col 27]) file dumb  at line 3 col 3,CString False
"\n" file dumb  at line 17 col 12]) [Comment "\nOrignal header from
captured packets.\n\nPOST /smp_18_ HTTP/1.1\nHost:
192.168.219.6:7676\nSOAPAction:
\"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"\nAccept-Language:
en-us;q=1, en;q=0.5\nAccept-Encoding: gzip\nContent-Type: text/xml;
charset=\"utf-8\"\nUser-Agent: gupnp-av-cp GUPnP/0.20.12
DLNADOC/1.50\nConnection: Keep-Alive\nContent-Length: 1405\n"]

 λ: onContent (Text.XML.HaXml.Combinators.deep $ tag "CurrentURI") doc
 -- this result is not what I want.
Document (Prolog (Just (XMLDecl "1.0" Nothing Nothing)) [] Nothing [])
[] (Elem (N "CurrentURI") [] [CString False "\n" file dumb  at line 7
col 19,CMisc (Comment " #EXTM3U ") file dumb  at line 8 col 1,CMisc
(Comment " #EXTINF:5850,\52628\49437\53945\49440\50689\54868
\47609\47329\44284\44053 140909 HDTV x264 720p AC3 \50864\47532\47568
\45433\51020 - nature boy ") file dumb  at line 9 col 1,CMisc
(Comment " http://192.168.219.2:8200/MediaItems/587.mkv ") file dumb
at line 10 col 1,CMisc (Comment " #EXTM3U ") file dumb  at line 11 col
1,CMisc (Comment "
#EXTINF:6411,The.Adventures.of.Tintin.2011.1080p.BluRay.x264.YIFY ")
file dumb  at line 12 col 1,CString False
"http://192.168.219.2:8200/MediaItems/165.mp4\n      " file dumb  at
line 13 col 1]) [Comment "\nOrignal header from captured
packets.\n\nPOST /smp_18_ HTTP/1.1\nHost:
192.168.219.6:7676\nSOAPAction:
\"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"\nAccept-Language:
en-us;q=1, en;q=0.5\nAccept-Encoding: gzip\nContent-Type: text/xml;
charset=\"utf-8\"\nUser-Agent: gupnp-av-cp GUPnP/0.20.12
DLNADOC/1.50\nConnection: Keep-Alive\nContent-Length: 1405\n"]
 λ:
```

How can I write filter to replace `CurrentURI` tag?

1. find 'CurrentURI' tag
2. replace content with some string
3. return modified whole document not just specific tag

Or, Where can I found tutorial or example for haxml? Yes, I'm reading
now haxml document. But needs some overview document.

Any advice would be appreciated.

Thanks.


More information about the Beginners mailing list