An XML Parser/DOM for Rebol 3. From the REBOL Console:
do %r3xml.r
? load-xml
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 "°F"]
sky: weather/get #text
]
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"
]
Wraps the above document in an object! with accessor functions. Values from the accessor functions are similarly wrapped in objects.
Returns a block of child nodes where the tag matches the 'tag value.
body: doc/get-by-tag <body>
body/get-by-tag <p>
Returns the first matching child node that has an attribute id matching the the 'id
value.
header: doc/get-by-id "header"
Returns a block of child nodes.
body/children
Returns an adjacent node or none!
header/sibling/after
Returns the value of an immediate child node of name 'name
(attribute or child tag)
doc/get #b
doc/get <a>
Returns the textual value of a tag node.
header/text
The value of a node.
header/value
Serializes the document back to an XML string.
Select nodes and values using a path notation:
; all titles in an RSS feed
rss/path [<rss> <channel> <item> <title> ?]
; all item nodes in an XML document
rss/path [* <item>]
; first header in an HTML document
html/path [<html> * <h1> 1]
Notation includes:
<rss> - select tags at the current depth
#version - select attributes at the current depth
* <item> - select any descendant with this tag
2 - selects the second result
? - converts the selection(s) to that node's value
To follow. Will return a node's parent node.