XML and REBOL
Source: AltXML | AltXML for R3
From the REBOL Console:
do http://www.ross-gill.com/r/altxml.r
? load-xml
Experimental version for REBOL 3:
do http://www.ross-gill.com/r/r3xml.r
Sample Usage
To get all the links from an xhtml page:
html: load-xml/dom http://w3.org
links: html/get-by-tag <a>
foreach link links [print ["(" link/get #href ")" link/text]]
To get entry titles from an RSS feed:
rss: load-xml/dom http://www.rebol.com/article/carl-rss.xml
entries: rss/get-by-tag <item>
foreach entry entries [probe entry/get <title>]
Today’s Weather
weather: http://weather.yahooapis.com/forecastrss?p=35205
weather: load-xml/dom weather
weather: pick weather/get-by-tag <condition> 1
probe weather: context [
temp: rejoin [
weather/get #temp
either system/version/4 = 2 ["^(A1)"]["^(B0)"] "F"
]
sky: weather/get #text
]
load-xml
This function parses an XML document provided by string!, url! or file! producing a representative block. Tags are represented by the tag! type, attributes by the issue! type and text prefixed by a /text refinement.
>> load-xml "<a b='c'>D</a>"
== [
<a> [
#b "c"
%.txt "D"
]
]
Tags with only text as children have a single value:
>> load-xml "<a>B</a>"
== [
<a> "B"
]
/dom refinement
Wraps the above document in an object! with accessor functions. Values from the accessor functions are similarly wrapped in objects.
get-by-tag ‘tag
Returns a block of child nodes where the tag matches the ‘tag value.
body: doc/get-by-tag <body>
body/get-by-tag <p>
get-by-id ‘id
Returns the first matching child node that has an attribute id matching the the ‘id value.
header: doc/get-by-id "header"
children
Returns a block of child nodes.
body/children
sibling /before /after
Returns an adjacent node or none!
header/sibling/after
get ‘name
Returns the value of an immediate child node of name ‘name (attribute or child tag)
doc/get #b
doc/get <a>
text
Returns the textual value of a tag node.
header/text
value
The value of a node.
header/value
flatten
Serializes the document back to an XML string.
Not implemented:
parent
To follow. Will return a node’s parent node.
Posted by Chris-RG, 18 days ago