作者:傲气战歌网 来源:www.27zg.com 发表时间:2014-03-24 03:02
Now we'd like to change the author names so it's in a "Last name, First name" format instead of "First name Last name". In our small file it would be easy to just change it, but suppose we had an XML file with thousands of entries, it would be tedious having to change it all manually. Luckily we can again use PHP for this purpose as we will now be looking at how you change data. The new data will be stored in books3.xml.
<?php $dom = new DOMDocument(); $dom->load('books.xml'); $xpath = new DOMXPath($dom); $authors = $xpath->query('book/author'); foreach($authors as $author) { $a = $author->nodeValue; // shortcut $last_name = substr($a, strrpos($a, ' ')+1); $first_name = substr($a, 0, strrpos($a, ' ')); $author->nodeValue = "{$last_name}, {$first_name}"; } $dom->save('books3.xml'); ?>We start the same way as before. The XPath query this time says "get all elements called 'author' which are children of an element called 'book'".
The value of a node can be accessed through its property nodeValue. We do some simple switching around and change the node value for each author element. Finally we save again.
Deleting dataThis time we want to make elements called author_firstname and author_lastname and then delete author. We'll save it in books4.xml.
<?php $dom = new DOMDocument(); $dom->load('books.xml'); $xpath = new DOMXPath($dom); $books = $xpath->query('book'); foreach($books as $book) { $author = $book->getElementsByTagName('author')->item(0); $a = $author->nodeValue; // shortcut $last_name = substr($a, strrpos($a, ' ')+1); $first_name = substr($a, 0, strrpos($a, ' ')); $book->removeChild($author); $book->appendChild($dom->createElement('author_firstname', $first_name)); $book->appendChild($dom->createElement('author_lastname', $last_name)); } $dom->save('books4.xml'); ?>I believe the code is pretty self-explanatory. DOMElement::removeChild() removes a child and DOMElement::appendChild() appends a child. Note that you need an instance of an element in order to remove it from another element.
There is another method called DOMElement::replaceChild() which takes two elements: the new and the old element.
Parsing data with DOMYou can use DOM to parse data as well, but it is easier to use SimpleXML. I'll show you anyways though. We'll use books4.xml to create a table similar to the one we created using SimpleXML with books.xml.
<?php $dom = new DOMDocument(); $dom->load('books4.xml'); $xpath = new DOMXPath($dom); $books = $xpath->query('book'); echo <<<EOF <h1>Book listing</h1> <table> <tr> <th>Title</th> <th>Author</th> <th>Publisher</th> <th>Price at Amazon.com</th> <th>ISBN</th> </tr> EOF; foreach($books as $book) { echo <<<EOF <tr> <td>{$book->getElementsByTagName('title')->item(0)->nodeValue}</td> <td>{$book->getElementsByTagName('author_firstname')->item(0)->nodeValue} {$book->getElementsByTagName('author_lastname')->item(0)->nodeValue}</td> <td>{$book->getElementsByTagName('publisher')->item(0)->nodeValue}</td> <td>\${$book->getElementsByTagName('amazon_price')->item(0)->nodeValue}</td> <td>{$book->getAttribute('isbn')}</td> </tr> EOF; } echo '</table>'; ?>As you see, SimpleXML is easier to use for parsing XML data. Where we could get the title like this in SimpleXML:
$book->title;We have to use this with DOM:
$book->getElementsByTagName('title')->item(0)->nodeValue;On the next page I'll show you how to tidy up your XML code.
Tidying up the codeIf we take a look at the code generated for books4.xml then it doesn't look very nice:
<?xml version="1.0"?> <books> <book isbn="978-1594489501"> <title>A Thousand Splendid Suns</title> <publisher>Riverhead Hardcover</publisher> <amazon_price>14.27</amazon_price> <author_firstname>Khaled</author_firstname><author_lastname>Hosseini</author_lastname></book> <book isbn="978-1594489587"> <title>The Brief Wondrous Life of Oscar Wao</title> <publisher>Riverhead Hardcover</publisher> <amazon_price>14.97</amazon_price> <author_firstname>Junot</author_firstname><author_lastname>Diaz</author_lastname></book> <book isbn="978-0545010221"> <title>Harry Potter and the Deathly Hallows</title> <publisher>Arthur A. Levine Books</publisher> <amazon_price>19.24</amazon_price> <author_firstname>J. K.</author_firstname><author_lastname>Rowling</author_lastname></book> </books>下一篇:常用RSS阅读器评测