Implementation code for parsing phpDOMElement to operate xml documents _ php instance

Source: Internet
Author: User
This article analyzes the implementation of the DOMElement operation xml document in php. For more information, see The code is as follows:


/*




<班级>
<学生 number="101">
<名字> Sun Wukong
<名字> Sun Walker
<年龄> 123
<介绍>&*$%特殊字串^&#$&

<学生 number="10" 2">
<名字> Bai Gujing
<年龄> 140
<介绍> Introduction


*/
$ 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 );


The 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
/*



1001
$200
Daming
Tianlong Babu


1002
$321
Zhang San
Swordsman


1004
$182
Li Si
Reader


*/
$ 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'],'
';
}

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.