标签:
1.xml概念注:如果有大段文本包含特殊字符,可以用CDATA(微信开发xml就有这个东东)来存放
XML文件
<?xml version="1.0" encoding="UTF-8"?> <books> <book> <name>php高级编程</name> <price>12</price> </book> <book> <name>php低级编程</name> <price>11</price> </book> </books>
DOM解析XML
$dom = new DOMDocument('1.0', 'utf-8'); //创建DOM解析对象 $dom->load('book.xml'); //载入解析文档文件 $books = $dom->getElementsByTagName('book'); //得到所有书本的DOMNodeList对象列表 //获取节点内容 echo '有几本书:'.$books->length."<br/>"; foreach ($books as $book) { $bookname = $book->getElementsByTagName('name'); //得到书本的名称DOMNodeList对象列表 $bookprice = $book->childNodes->item(3); //得到书本的价格DOMElement对象,childNodes包含换行节点 echo 'bookname:'.$bookname->item(0)->nodeValue."<br/>"; echo 'bookprice:'.$bookprice->nodeValue.'<br/>'; // $bookprice->nodeValue = 15; //修改节点内容 } //添加节点内容 $cbook = $dom->createElement('book'); $cbookname = $dom->createElement('name'); $cbookname_text = $dom->createTextNode('php中级编程'); $cbookname->appendChild($cbookname_text); $cbook->appendChild($cbookname); $dom->getElementsByTagName('books')->item(0)->appendChild($cbook); //删除节点内容 $book3 = $dom->getElementsByTagName('book')->item(2); //获取对象DOMElement对象 $book3->parentNode->removeChild($book3); //根据父亲节点对象移除获取的子DOMElement对象 $dom->save('book.xml');
标签:
原文地址:http://blog.csdn.net/qivan/article/details/43734411