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

XML parsing and creation examples

little bottle
little bottleforward
2019-04-23 15:23:016027browse

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(&#39;1.0&#39;,&#39;UTF-8&#39;);
    //print_r($dom); //DOMDocument Object
    $dom->load(&#39;demo.xml&#39;); //载入要解析的xml文档
    $titleList = $dom->getElementsByTagName(&#39;title&#39;); ////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 &#39;<br>&#39;;
    //也可以直接写成连贯操作:  
    $text = $dom->getElementsByTagName(&#39;title&#39;)->item(0)->childNodes->item(0)->wholeText;
    echo $text; //php
    echo &#39;<br>&#39;;
    //第二种方法:
    $text = $dom->getElementsByTagName(&#39;title&#39;)->item(0)->nodeValue; //获取到文本内容
    echo $text; //php

In addition to parsing, you can also delete and replace xml nodes:

    $dom = new DOMDocument(&#39;1.0&#39;,&#39;UTF-8&#39;);
    $dom->load(&#39;demo.xml&#39;); //载入要解析的xml文档
    /**
     *xml删除节点
     * */
    $php = $dom->getElementsByTagName(&#39;book&#39;)->item(0); //获取文本节点
    $php->parentNode->removeChild($php); //获取文本节点的父节点,然后站在父节点的立场删除子文本节点
    /**
     * xml替换节点
     * */
    $java = $dom->getElementsByTagName(&#39;title&#39;)->item(0); //获取要替换的文本节点
    $newnode = $dom->createTextNode(&#39;python&#39;); //创建新的文本节点
    $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(&#39;1.0&#39;,&#39;UTF-8&#39;);
    $text =  $dom->createTextNode(&#39;php&#39;); //创建文本节点
    $title = $dom->createElement(&#39;title&#39;); //创建普通节点
    $title->appendChild($text); //将文本节点作为子节点添加到普通节点内
    $cdata = $dom->createCDATASection(&#39;PHP是世界上最好的编程语言.emmm...&#39;); //创建CDATA节点
    $author = $dom->createElement(&#39;author&#39;); //创建普通节点
    $author->appendChild($cdata); //将CDATA节点作为子节点添加到普通节点内
    $book = $dom->createElement(&#39;book&#39;); //创建普通节点
    //将上面创建的两个普通节点作为子节点添加到普通节点内
    $book->appendChild($title);
    $book->appendChild($author);
    $book_id = $dom->createAttribute(&#39;book_id&#39;); //创建属性名
    $book_id->value = &#39;1&#39;; //创建属性值
    $book->appendChild($book_id); //将属性加到普通节点内
    $bookstore = $dom->createElement(&#39;bookstore&#39;); //创建普通节点
    $bookstore->appendChild($book); //将普通节点作为子节点添加到普通节点内
    $dom->appendChild($bookstore); //将普通节点添加到文档内
    // header("content-type:text/xml");
    // echo $dom->saveXML(); //直接输出xml文本
    echo $dom->save(&#39;demo.xml&#39;); //生成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