Parse the implementation code of the phpDOMElement operation xml document. Copy the code as follows :*? Xmlversion1.0encodingUTF-8 standaloneno ?! -- Css style definition, without adding some points. For example: name {color: red ;}--? Xml-stylesheettypetextcsshre
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'],'
';
}
The http://www.bkjia.com/PHPjc/326981.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326981.htmlTechArticle 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" hre...