Creating and parsing XML in PHP

Posted by ESCForums.com on Tue, 17 Sep 2019 10:18:02 +0200

Creating and parsing XML in PHP
I. Manipulating XML with SimpleXML
There are two traditional ways to deal with XML files: SAX and DOM.SAX scan the XML files once based on event triggering mechanism to complete the processing; DOM constructs the whole XML file as a DOM tree and processes it through traversing the DOM tree. Both methods have their advantages and disadvantages. SAX's processing ideas are relatively abstract, and DOM's advantages and disadvantages are relatively abstract. The process is relatively cumbersome and not suitable for beginners.
PHP5 exits a new set of XML processing functions, namely SimpleXML. SimpleXML itself is, as it is, compact and compact, providing only a few methods of functions. It is very powerful and simple to handle XML files with it alone.

1. Create an XML file
[PHP] Pure Text View Copy Code
?

<?php
$_xml=<<<xml
<?xml version="1.0" encoding='utf-8'?>
<root>

  <version>1.0</version>
  <info>xml Analytical testing</info>
  <user>
      <author sex="male">Zhang San</author>
      <age>23</age>
  </user>
  <user>
      <author sex="female">Li Si</author>
      <age>24</age>
  </user>
  <user>
      <author sex="male">Wang Wu</author>
      <age>25</age>
  </user>

</root>
xml;
// Creating Objects to Resolve xml Strings
$_sxe = new SimpleXMLElement($_xml);
// Generate an XML file in the current directory
$_sxe->asXML('./test.xml');
?>

Design sketch:

2. Loading an XML file
[PHP] Pure Text View Copy Code
?

<?php

$_sxe = simplexml_load_file("./test.xml");
var_dump($_sxe);//Output of relevant information
print_r($_sxe);//Output of main information
Reflection::export(new ReflectionClass($_sxe));//See details with reflection

?>
Design sketch:

3. Parsing XML files
[PHP] Pure Text View Copy Code
?

<?php

 header("Content-type:text/xml");
 $_sxe = simplexml_load_file("test.xml");//Load an XML file
 echo $_sxe->asXML();//Print the whole XML

?>

Design sketch:

4. Read out XML data
[PHP] Pure Text View Copy Code
?

<?php

  $_sxe = simplexml_load_file("test.xml");//Load an XML file
  //Read the value of a first-level node, such as the version tag
  echo $_sxe->version;
  echo '<hr/>';
  //If there are more than one, you can set its numeric subscript
  echo $_sxe->version[0];
  echo '<hr/>';
  //If you want to print it all out, you can use traversal
  foreach($_sxe->version as $_version){
      echo '['.$_version.']';
  }
  //author accessing secondary nodes
  echo $_sxe->user[0]->author;
  echo '<hr/>';

  //Get the name value of all secondary nodes
  foreach($_sxe->user as $_user){
      echo '[' . $_user->author .']';
  }
  echo '<hr/>';
   
  //Get the attributes of the label of the secondary node
  echo $_sxe->user[0]->author->attributes();

?>

Design sketch:

5. Use XPath to get nodes
[PHP] Pure Text View Copy Code
?

<?php

 $_sxe = simplexml_load_file("test.xml");//Load an XML file
 //Use Xpath to get node information
 $_verson = $_sxe->xpath("/root/version");
 echo $_version[1];
 //Traversing version
 foreach($_version as $_v){
     echo '[' . $_v .']';
 }
 //Accessing secondary nodes
 $_user = $_sxe->xpath('/root/user');
 echo $_user[2]->name;
 //Traversing secondary nodes
 foreach($_user as $_u){
     echo '['. $u->name .']';
 }
 //Access attribute
 echo $_user[1]->author->attributes();

?>

Design sketch:

II. Using DOMDocument to manipulate XML
In many cases, hand-generated tags require documents to be generated from top to bottom. It is necessary to ensure that tags are complete, start and end tags. Although some PHP functions or classes can be improved, PHP also provides a more helpful set of built-in objects and functions. Document Object Model (DOM) provides one. A stump structure that makes it easy to create and process labels.
1.DOMDocument Generates XML
[PHP] Pure Text View Copy Code
?

<?php

//Statement xml
$_doc = new DOMDocument('1.0','utf-8');
//Typesetting format
$_doc->formatOutput = true;
//Create a master tag
$_root = $_doc->createElement('root');    
//Create a first-level label version
$_version = $_doc->createElement('version');
//Assign values to version Tags
$_versionTextNode = $_doc->createTextNode('1.0');
//Put the value in the version tag
$_version->appendChild($_versionTextNode);
//Put the first-level label version into the root
$_root->appendChild($_version);
//Write the main tag to xml
$_doc->appendChild($_root);
//Generate xml
$_doc->save('aaa.xml');

?>
Design sketch:

2.DOMDocument parses XML
[PHP] Pure Text View Copy Code
?
<?php

//Create a DOMDocument()
$_doc = new DOMDocument();

//Load xml
$_doc->load('test.xml');

//Take version tag
$_version =$_doc->getElementsByTagName('version');;
echo $_version->item(2)->nodeValue;
//Traversing version Tags
foreach($_version as $v){
    echo $v->nodeValue;
}

?>
Design sketch

More technical information: gzitcast

Topics: PHP xml encoding Attribute