Parse php DOMElement implementation code for operating xml documents

Source: Internet
Author: User

Copy codeThe Code is as follows:
/* <? Xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
<! -- Css style definition, without adding some points. For example: name {color: red;} -->
<? Xml-stylesheet type = "text/css" href = "css.css"?>
<! -- Introduce the dtd document definition file (root element: Class) <! DOCTYPE class SYSTEM "class. dtd"/> -->
<! -- <! DOCTYPE class [
<! ELEMENT class (student +)>
<! ELEMENT student (name, age, introduction)>
<! ELEMENT name (# PCDATA)>
<! ELEMENT age (# PCDATA)>
<! ELEMENT introduction (# PCDATA)>
]/> -->
<Class>
<Student number = "101">
<Name> Sun Wukong </Name>
<Name> sun Walker </Name>
<Age> 123 </age>
<Introduction> <! [CDATA [& * $ % special string ^ & # $ &]> </introduction>
</Student>

<Student number = "10" 2 ">
<Name> White Bone essence </Name>
<Age> 140 </age>
<Introduction> introduction </introduction>
</Student>
</Class>
*/
$ Xmldoc = new DOMDocument ('1. 0', 'utf-8 ');
$ Xmldoc-> load ('datas. xml ');

$ ItemsNodeList = $ xmldoc-> getElementsbyTagName ('studen ');
$ ItemElement = $ itemsNodeList-> item (0); // obtain the first complete student information node.
$ ItemChildsNodeList = $ itemElement-> getElementsbyTagName ('name'); // obtain the subnode "name", which may have multiple names
$ ItemChildNode = $ itemChildsNodeList-> item (0); // obtain the First Name node.
Echo $ itemChildNode-> nodeValue; // output node Value

// Encapsulate it into a function
$ NodeArr = array ('name', 'age', 'Introduction ');
Function getNodeVal ($ xmldoc, $ itemsName, $ nodeArr ){
$ Items = $ xmldoc-> getElementsByTagName ($ itemsName );
For ($ I = 0; $ I <$ items-> length; $ I ++ ){
$ Item = $ items-> item ($ I );
Foreach ($ nodeArr as $ node ){
$ Data [$ I] [] = $ item-> getElementsByTagName ($ node)-> item (0)-> nodeValue;
}
}
Return $ data;
}

$ Data = getNodeVal ($ xmldoc, 'student ', $ nodeArr );
Print_r ($ data );

Copy codeThe Code is as follows:
// Add a node
$ Xmldoc = new DOMDocument ('1. 0', 'utf-8 ');
$ Xmldoc-> load ('datas. xml ');
$ Items = $ xmldoc-> getElementsByTagName ('class')-> item (0); // Root Node
$ Student = $ xmldoc-> createElement ('studen'); // create a new student Node
$ Stu_name = $ xmldoc-> createElement ('name', 'zhang san ');
$ Stu_age = $ xmldoc-> createElement ('age', '15 ');
$ Stu_intro = $ xmldoc-> createElement ('Introduction', 'strong hands-on ability and stable scores ');
$ Items-> appendChild ($ student );
$ Student-> appendChild ($ stu_name );
$ Student-> appendChild ($ stu_age );
$ Student-> appendChild ($ stu_intro );
$ Bytes = $ xmldoc-> save ('datas. xml ');
Echo ($ bytes )? "Written: $ bytes": 'failed to save ';

// Delete a node
$ Xmldoc = new DOMDocument ('1. 0', 'utf-8 ');
$ Xmldoc-> load ('datas. xml ');
$ Student = $ xmldoc-> getElementsByTagName ('studen')-> item (2); // locate the node to be deleted.
$ Student-> parentNode-> removeChild ($ student); // delete a parent node
$ Xmldoc-> save ('datas. xml ');

// Modify the node Value
$ Student = $ xmldoc-> getElementsByTagName ('studen')-> item (2 );
$ Student-> getElementsByTagName ('age')-> item (0)-> nodeValue + = 10;
$ Student-> setAttribute ('id', '123 ');
$ Xmldoc-> save ('datas. xml ');

// Apply Xpath to find nodes

$ Xml = new DOMDocument ('1. 0', 'utf-8 ');
$ Xml-> load ('dat. xml ');
$ Xpath = new DOMXPath ($ xml );
$ NodeList = $ xpath-> query ('/aaa/bbb/ddd/fff ');
Echo $ nodeList-> item (0)-> nodeValue;

// SimpleXML class operation xml
/*
<? Xml version = "1.0" encoding = "UTF-8"?>
<Books>
<Book house = "Tsinghua Publishing">
<Code> 1001 </code>
<Price> 200 RMB </price>
<Author> Daming </author>
<Title> tianlong Babu </title>
</Book>
<Book house = "Peking University Publishing">
<Code> 1002 </code>
<Price> 321 RMB </price>
<Author> Zhang San </author>
<Title> swordsman </title>
</Book>
<Book house = "published by people">
<Code> 1004 </code>
<Price> 182 RMB </price>
<Author> Li Si </author>
<Title> reader </title>
</Book>
</Books>
*/
$ Xml = simplexml_load_file ('books. xml ');
$ Books = $ xml-> book;
Echo $ books [1]-> title. $ books [1] ['house']; // direct to the second book
Foreach ($ xml as $ item ){
Echo $ item-> title, '', $ item ['house'], '<br/> ';
}

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.