Home>Article>Backend Development> XML parsing and creation examples

XML parsing and creation examples

little bottle
little bottle forward
2019-04-23 15:23:01 5861browse

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:

   php php入门到放弃   java java入门到放弃   C C入门到放弃 

Use PHP to parse the content in demo.xml, take the text node of php as an example:

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 '
'; //也可以直接写成连贯操作: $text = $dom->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText; echo $text; //php echo '
'; //第二种方法: $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:

   php  

PHP code:

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete