Day 19 – Document Object Model

Zend PHP Certification

I can’t believe I’m spending so much time on this XML subject (granted it’s my weakest area as I haven’t had a chance to use it yet).  I think tomorrow I’ll implement the XML manipulation of iTunes AppStore data (that will make a LOT of people happy).

Anyway, it’s important to note, when we get into this section, that the DOM is apparently interoperable with the SimpleXML object.  This is good to know.  The method to do so is:

// SimpleXML to DOM
  1. $sxml = simplexml_load_file('text.xml');
  2. $node = dom_import_simplexml($sxml);
  3.  
  4. $dom = new DOMDocument();
  5. $dom->importNode($node, true);
  6. $dom->appendChild($node);
  7.  
  8. // DOM to SimpleXML
  9. $dom = new DOMDocument();
  10. $dom->load('test.xml');
  11.  
  12. $sxml = simplexml_import_dom($dom);

Okay, well before we jump the gun with the DOM syntax, here is the basic code to open a new DOM object:

// Load from a file
  1. $dom = new DomDocument();
  2. $dom->load("library.xml");
  3.  
  4. // Load from a string
  5. $dom = new DomDocument();
  6. $dom->load($xml);
  7.  
  8. // Save the document
  9. $dom->save('library.xml');
  10.  
  11. // Output to the screen
  12. $dom->saveXML();

XPath Queries are mode advanced than SimpleXML and can be a little more work:

$xpath = new DomXPath($dom);
  1.  
  2. $result = $xpath->query("//title/text()");
  3.  
  4. // Single access
  5. echo $result->item(0)->data;
  6.  
  7. // Loop
  8. foreach ($result as $book) {
  9.   echo $book->data;
  10. }

Modification of data is a little bit more complex than before.  In DOM, the objects you create create are not automatically assigned to the XML and have to be appended later.

// Create the book element
  1. $book = $dom->createElement("book");
  2. // Add some attributes
  3. $book->setAttribute("isbn", "12345");
  4.  
  5. // Create the title child
  6. $title =  $dom->createElement("title");
  7. // Add text to it
  8. $text = $dom->createTextNode("Harry Potter");
  9.  
  10. // Add the text to the title, and the title to the book
  11. $title->appendChild($text);
  12. $book->appendChild($title);
  13.  
  14. // Add the book to the XML document itself
  15. $dom->documentElement->appendChild($book);

Remember in SimpleXML, it was impossible to fully remove a node.  Well, here is the possible version in DOM.

// Using XPath we can identify the item we want
  1. $xpath = new DOMDocument();
  2. $result = $xpath->query("//text");
  3.  
  4. // To remove an attribute, it's easy
  5. $result->item(0)->removeAttribute('type');
  6.  
  7. // But here is the confusing part: to remove the node,
  8. // we need to ask its parent to remove their child that is the node.
  9. $result->item(0)->parentNode->removeChild($result->item(0));
  10.  
  11. // And if we needed to clear CDATA, we can use this function
  12. $result = $xpath->query('text()');
  13. $result->item(0)->deleteData(0, $result->item(0)->length);

Tomorrow will be the Security chapter (almost done!!) and I’ll try to write up the PHP iTunes AppStore code (since it involves XML).

No Comments

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  • Donate

    If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

  • My Lifestream

  • License

    Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.