听dj战歌,就上傲气战歌网!2015年传奇家族玩家最喜爱的家族战歌网
战歌推荐:战歌网 战歌网dj Mc战歌网 DJ战歌网下载 激情战歌-冰雪战歌网 客服Q:350317
新闻搜索:

Handling XML data(3)

作者:傲气战歌网     来源: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 data

This 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 DOM

You 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 code

If 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>

数据统计中!!

最新评论共有  位网友发表了评论
发表评论(评论内容:请文明参与评论,禁止谩骂攻击!)
不能超过250字节,请自觉遵守互联网相关政策法规.
昵称:    发表评论 (Ctrl+Enter快速回复)

关于本站 | 合作加盟 | 合作说明 | 免责声明 | 广告服务 | 网站地图

健康游戏忠告:抵制不良游戏 拒绝盗版游戏 注意自我保护 谨防受骗上当 适度游戏益脑 沉迷游戏伤身 合理安排时间 享受健康生活

如有意见和建议,请惠赐E-mail至350317@qq.com 联系QQ:350317

Copyright © 2010-2013 Www.27zG.CoM
苏ICP备11049833号