This section discusses some of the details of the structure of the
trees created when parsing an XML document with the xmlTree
interface and how to use them.
The samples/xmlapi
directory contains the following code examples:
xmlTree01_New
- shows many ways of creating new
nodes in an XML treexmlTree02_Get
- shows ways of extracting information from
an XML documentxmlTree03_Sibling
- shows many of the tree traversal
mechanismsxmlTree04_Ns
- shows how to resolve and examine namespace
information in a treexmlTree05_SetNs
- shows how to create and set namespace
information in a treexmlTree06_Copy
- shows how to make copies of nodes in an
XML treexmlTree07_Encoding
- shows how to use various
input/output encodingsxmlTree08_XPath
- shows how to use XPath expressionsxmlTree09_DTD
- shows how to extract DTD informationxmlTree10_XSL
- shows how to apply an XSL stylesheet
Below is a small example program that shows how the xmlTree
API
can be used to open an XML document, add content to an element, add
another element, and print the output.
<capture>
<Result>
<Url>http://www.example.com/page.htm</Url>
<ResultTitle>Generic Page Title</ResultTitle>
</Result>
</capture><$xmlRaw=$ret>
<$doc = (xmlTreeNewDocFromString($xmlRaw, 'XML_PARSE_NOBLANKS'))>
<$root = (xmlTreeGetRootElement($doc))>
<$title = (xmlTreeGetChildren($root, 'ResultTitle'))>
<$titleText = (xmlTreeGetContent($title))>
Existing title is "$titleText"
<$ret = (xmlTreeAddContent($title, ", now with 12% more fun"))>
<$ret = (xmlTreeNewElement($root, 'Abstract', 'content content'))>
<$output = (xmlTreePrintDoc($doc, 'INDENT'))>
after changes doc is:
$output