PHP implementation of DOMDocument parsing and generating XML based on a detailed approach

Source: Internet
Author: User
This article mainly introduces PHP based on DOMDocument parsing and generation of XML methods, combined with specific examples of PHP using DOMDocument parsing XML nodes and XML file generation of various common operating skills, the need for friends can refer to the following

1. Generation of XML

DOMDocument operation XML is a bit more complicated than the previous simplexml, I think SimpleXML think of Java dom4j, anyway the principle is the same. If the node in the DOMDocument, the attribute as a leaf so DOMDocument DOMDocument is the root, the nodes and properties are mounted under this object. Look at the code below, it's clear.

<?php $doc =new DOMDocument (' 1.0 ', ' utf-8 '); Create root node $root = $doc->createelement ("Studentinfo"); Create the first child node $item = $doc->createelement ("item"); $name = $doc->createelement ("name", "Jolin Tsai"); $studentnum = $doc->createelement ("num", "2009010502"); Create an attribute (Phpdom can treat any element as a child node) $id = $doc->createattribute ("id"); $value = $doc->createtextnode (' 1 '); $id->appendchild ($value); Join the child node under the parent node $item->appendchild ($name); $item->appendchild ($studentnum); $item->appendchild ($id); $item 2= $doc->createelement ("Item"); $name 2= $doc->createelement ("name", "Wilber Cypress"); $studentnum 2= $doc->createelement ("num", "2009010505"); $id 2= $doc->createattribute ("id"); $value 2= $doc->createtextnode (' 2 '); $id 2->appendchild ($value 2); $item 2->appendchild ($name 2); $item 2->appendchild ($studentnum 2); $item 2->appendchild ($id 2); $root->appendchild ($item); $root->appendchild ($item 2); The root node is mounted on the DOMDocument object $doc->appendchild ($root); Header ("Content-type:text/xml"); Output XML echo $doc on the page->savexml (); Save XML as a file $doc->save ("Stu.xml");? >

This code is not complicated to look closely, the possibility in the id attribute of the place will be a bit of doubt, the text node must also be mounted under DOMDocument, then the text node is mounted under the properties. Look at the generated XML

In fact, DOMDocument is the first generation of nodes or attributes, and the hierarchical relationship of XML is finally embodied by Addchild.

2. DOMDocument parsing

<?php $doc =new DOMDocument (); If parsing the XML string, use Loadxml $doc->load (' stu.xml '); Get root node $root = $doc->documentelement; The corresponding element is obtained by the name of the tag $items = $root->getelementsbytagname (' Item '); foreach ($items as $key = + $val) {  //Echo count ($val->attributes);  The ID is the first attribute so using item (0), NodeValue is used to get the value of the node  echo $val->attributes->item (0)->name. ":". $val Attributes->item (0)->nodevalue. "  ";  foreach ($val->getelementsbytagname (' name ') as $key 2=> $val 2) {    echo $val 2->nodevalue. "  ";  }  foreach ($val->getelementsbytagname (' num ') as $key 3=> $val 4) {    echo $val 4->nodevalue. " </br> ";  } }?>

Parsing the words with emphasis on understanding, getElementsByTagName methods, attributes attributes, and item is the focus of parsing XML. Everything else is simple, look at the parsed thing.

Overall it's more trouble than SimpleXML, but as a programmer you can accept it.

Related recommendations:

PHP uses DOMDocument to manipulate XML methods

PHP XML Operation class DOMDocument

A solution to the problem of garbled Chinese in PHP DOMDocument Saving xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.