Home arrow XML arrow Page 2 - Using PHP with XML (part 2)

Meet Joe Cool - XML

PHP offers two methods of parsing an XML document. You've already seen how SAX works; in this article, find out how to use PHP's DOM functions to traverse an XML tree, and also learn about important differences between the two approaches.

TABLE OF CONTENTS:
  1. Using PHP with XML (part 2)
  2. Meet Joe Cool
  3. Building A Library
  4. Anyone For Chicken?
  5. Conclusions...
  6. ...And Links
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 3
June 20, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
As before, the first order of business is to read the XML data into memory. PHP offers three functions to do this: the xmldoc() and xmltree() functions accept a string containing XML data as argument, and build a tree structure from that string, while the xmldocfile() function accepts a filename as argument, and builds a DOM tree after reading the data in the file.

<? // create an XML-compliant string $XMLDataString = "<?xml version="1.0"?" . "><me><name>Joe Cool</name><age>24</age><sex>male</sex></me>"; // create a document object $XMLDoc = xmldoc($XMLDataString); // create a tree object $XMLTree = xmltree($XMLDataString); ?>
You can also load a file directly.

<? // data file $XMLFile = "me.xml"; // create a document object $XMLDoc = xmldocfile($XMLFile); ?>
Windows users should note that xmldocfile() needs the complete path to the file, including drive letter, to work correctly.{mospagebreak title=Parents And Their Children} Once the document has been read in, a number of methods become available to traverse the document tree.

If you're using a document object, such as the one returned by xmldoc() and xmldocfile(), you can use the following methods to obtain references to other nodes in the tree,

<? // data file $file = "library.xml"; // create a document object $dom = xmldocfile($file); // echo these values to see the object type // get root node $dom->root(); // get children under the root node as array $dom->children(); ?>
or to properties of the document itself.

<? // data file $file = "library.xml"; // create a document object $dom = xmldocfile($file); // get XML version echo $dom->version; // get XML encoding echo $dom->encoding; // get whether standalone file echo $dom->standalone; // get XML URL echo $dom->url; // get XML character set echo $dom->charset; ?>
Once you've obtained a reference to a node, a number of other methods and properties become available to help you obtain the name and value of that node, as well as references to parent and child nodes. Take a look:

<? // data file $str = "<?xml version="1.0"?><me><name>Joe Cool</name><age>24</age><sex>male</sex></me>"; // create a document object $dom = xmldoc($str); // get reference to root node $root = $dom->root(); // get name of node - "me" echo $root->name; // get children of node, as array $children = $root->children(); // get first child node $firstChild = $children[0]; // let's see a few node properties // get name of first child node - "name" echo $firstChild->name; // get content of first child node - "Joe Cool" echo $firstChild->content; // get type of first child node - "1", or "XML_ELEMENT_NODE" echo $firstChild->type; // go back up the tree! // get parent of child - this should be <me> $parentNode = $firstChild->parent(); // check it...yes, it is "me"! echo $parentNode->name; ?>
A quick note on the "type" property above: every node is of a specific type, and this property returns a numeric and textual code corresponding to the type. A complete list of types is available in the PHP manual.{mospagebreak title=Welcome To The Human Race} Many of the object methods you just saw are also available as regular functions; for example,

<? // data file $str = "<?xml version="1.0"?><me><name>Joe Cool</name><age>24</age><sex>male</sex></me>"; // create a document object $dom = xmldoc($str); // get reference to root node $root = $dom->root(); // get name of node - "me" echo $root->name; // you could also do this! // get reference to root node $root = domxml_root($dom); // get name of node - "me" echo $root->name; ?>
Finally, element attributes may be obtained using either the $node->attributes() object method, or the dom_attributes() function.

<? // data file $str = "<?xml version="1.0"?><me species="human"><name>Joe Cool</name><age>24</age><sex>male</sex></me>"; // create a document object $dom = xmldoc($str); // get reference to root node $root = $dom->root(); // get attribute collection // $attributes = $root->attributes(); // you could also do this! // $attributes = domxml_attributes($root); // get first attribute name - "species" echo $attributes[0]->name; // get first attribute value - "human" echo $attributes[0]->children[0]->content; // you could also do this! // echo domxml_getattr($root, "species"); ?>


 
 
>>> More XML Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: