Home >Backend Development >XML/RSS Tutorial >XML parsing and creation examples
XML is used to transmit and store data. How to parse the obtained XML text? This article talks about the parsing and creation of xml text. It has certain reference value. Interested friends can come and learn about it.
1. Parse XML
Create demo.xml file:
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book book_id="1"> <title>php</title> <author>php入门到放弃</author> </book> <book book_id="2"> <title>java</title> <author>java入门到放弃</author> </book> <book book_id="3"> <title>C</title> <author>C入门到放弃</author> </book></bookstore>
Use PHP to parse the content in demo.xml, take the text node of php as an example:
<?php /*思路:1.先载入xml文档 2.获取节点列表对象 3.获取子节点对象 4.获取子节点列表对象 5.获取文本对象 如果层级比较深就是循环2-4步,先拿列表对象,再拿子节点对象...*/ $dom = new DOMDocument('1.0','UTF-8'); //print_r($dom); //DOMDocument Object $dom->load('demo.xml'); //载入要解析的xml文档 $titleList = $dom->getElementsByTagName('title'); ////DOMNodeList Object ( [length] => 3 ) 获取节点列表对象 $title = $titleList->item(0); //DOMElement Object 获取子节点对象 $title = $title->childNodes; //DOMNodeList Object ( [length] => 1 )获取子节点列表对象 $title = $title->item(0); //DOMText Object 获取到文本对象 $text = $title->wholeText; //获取到文本内容 echo $text; //php echo '<br>'; //也可以直接写成连贯操作: $text = $dom->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText; echo $text; //php echo '<br>'; //第二种方法: $text = $dom->getElementsByTagName('title')->item(0)->nodeValue; //获取到文本内容 echo $text; //php
In addition to parsing, you can also delete and replace xml nodes:
$dom = new DOMDocument('1.0','UTF-8'); $dom->load('demo.xml'); //载入要解析的xml文档 /** *xml删除节点 * */ $php = $dom->getElementsByTagName('book')->item(0); //获取文本节点 $php->parentNode->removeChild($php); //获取文本节点的父节点,然后站在父节点的立场删除子文本节点 /** * xml替换节点 * */ $java = $dom->getElementsByTagName('title')->item(0); //获取要替换的文本节点 $newnode = $dom->createTextNode('python'); //创建新的文本节点 $java->replaceChild($newnode,$java->firstChild); //用新节点替换旧文本节点内容 header("content-type:text/xml"); echo $dom->saveXML(); //直接输出xml文本
2. Create XML
Use PHP to create an XML file as follows:
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book book_id="1"> <title>php</title> <author><![CDATA[PHP是世界上最好的编程语言.emmm...]]></author> </book></bookstore>
PHP code:
<?php $dom = new DOMDocument('1.0','UTF-8'); $text = $dom->createTextNode('php'); //创建文本节点 $title = $dom->createElement('title'); //创建普通节点 $title->appendChild($text); //将文本节点作为子节点添加到普通节点内 $cdata = $dom->createCDATASection('PHP是世界上最好的编程语言.emmm...'); //创建CDATA节点 $author = $dom->createElement('author'); //创建普通节点 $author->appendChild($cdata); //将CDATA节点作为子节点添加到普通节点内 $book = $dom->createElement('book'); //创建普通节点 //将上面创建的两个普通节点作为子节点添加到普通节点内 $book->appendChild($title); $book->appendChild($author); $book_id = $dom->createAttribute('book_id'); //创建属性名 $book_id->value = '1'; //创建属性值 $book->appendChild($book_id); //将属性加到普通节点内 $bookstore = $dom->createElement('bookstore'); //创建普通节点 $bookstore->appendChild($book); //将普通节点作为子节点添加到普通节点内 $dom->appendChild($bookstore); //将普通节点添加到文档内 // header("content-type:text/xml"); // echo $dom->saveXML(); //直接输出xml文本 echo $dom->save('demo.xml'); //生成xml文件
Output in the browser as shown below:
Related tutorials: XML video tutorial
The above is the detailed content of XML parsing and creation examples. For more information, please follow other related articles on the PHP Chinese website!