An instance of XMLDocument represents an XML document in memory. The XMLDocument component wraps a DOM (Document Object Model) tree structure. It contains methods to initialize, modify and print the DOM structure. The XMLDocument component is also an entry point to the root XML node of the DOM tree.
xmlString =
"<claims group=\"test\">\n"+
" <claim id=\"1\">\n"+
" <header>\n"+
" <date>2005-05-17</date>\n"+
" <insured>\n"+
" <firstname>John</firstname>\n"+
" <lastname>Smith</lastname>\n"+
" </insured>\n"+
" </header>\n"+
"\n"+
" <body>\n"+
" <description>This claim from Smith</description>\n"+
" <items>\n"+
" <item id=\"1\">Blah 1 blah blah</item>\n"+
" <item id=\"2\">Blah 2 blah blah</item>\n"+
" <item id=\"3\">Blah 3 blah blah</item>\n"+
" </items>\n"+
" </body>\n"+
" </claim>\n"+
"\n"+
" <claim id=\"2\">\n"+
" <header>\n"+
" <date>2005-05-18</date>\n"+
" <insured>\n"+
" <firstname>Pete</firstname>\n"+
" <lastname>Jackson</lastname>\n"+
" </insured>\n"+
" </header>\n"+
"\n"+
" <body>\n"+
" <description>This claim from Jackson </description>\n"+
" <items>\n"+
" <item id=\"1\">Blah 11 blah blah</item>\n"+
" <item id=\"2\">Blah 22 blah blah</item>\n"+
" <item id=\"3\">Blah 33 blah blah</item>\n"+
" </items>\n"+
" </body>\n"+
" </claim>\n"+
"</claims>"
// parse the XML and get the Root node
// NOTE: the 'content' String may be a URL to a file. Example:
// XMLDocument.create(content : "file://test.xml")
xmlDocument as XMLDocument = XMLDocument.create(content : xmlString)
claims as XMLNode = rootFor(xmlDocument)
// Get the description of all claims for "Jackson", using XPath
claimsFromJackson as XMLNode[] = getXMLNodesFrom(claims,
xpath : "/claims/claim[header/insured/lastname=\"Jackson\"]/body/description")
for each description in claimsFromJackson do
display description.text
end
// Display the attributes of the root node (<claims> node)
display claims.attributes
// Display the "id" attribute of each claim
for each claim in claims.children do
display claim.attributes["id"]
end
// Get the description of all claims for "Jackson", using XPath
claimsFromJackson = getXMLNodesFrom(claims,
xpath : "/claims/claim[header/insured/lastname=\"Jackson\"]/body/description")
for each description in claimsFromJackson do
display description.text
end
xmlDoc = XMLDocument()
rootNode = addRootFor(xmlDoc, tag : "myroot")
rootNode.attributes["id"] = "testing"
addChildFor(rootNode, tag : "firstchild")
child = addChildFor(rootNode, tag : "secondchild")
child.text = "2nd Child"
xmlString = print(xmlDoc, properties : null)
// xmlString contains:
// <?xml version="1.0" encoding="UTF-8"?>
// <myroot id="testing">
// <firstchild/>
// <secondchild>2nd Child</secondchild>
// </myroot>