Implementation code for parsing php DOMElement and manipulating xml documents_PHP tutorial

WBOY
Release: 2016-07-21 15:11:10
Original
761 people have browsed it

Copy code The code is as follows:

/*






< Name>Sun Wukong
Sun Xingzhe
123
< ![CDATA[&*$%Special String^&#$&]]>


White Bone Essence
140
Introduction content


*/
$xmldoc = new DOMDocument('1.0', 'UTF-8 ');
$xmldoc->load('datas.xml');

$itemsNodeList = $xmldoc->getElementsbyTagName('Student');
$itemElement = $itemsNodeList->item(0);//Get the first complete student information node
$itemChildsNodeList = $itemElement->getElementsbyTagName('name');//Get the child node "name", there may be multiple names
$itemChildNode = $itemChildsNodeList->item(0);//Get the first name Node
echo $itemChildNode->nodeValue;//Output node value

//Encapsulated 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 node
$xmldoc = new DOMDocument('1.0', 'UTF-8');
$xmldoc->load('datas.xml');
$items = $xmldoc->getElementsByTagName('Class')->item(0);//Root node
$student = $xmldoc->createElement('Student');//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 performance');
$items->appendChild($student);
$student->appendChild($stu_name );
$student->appendChild($stu_age);
$student->appendChild($stu_intro);
$bytes = $xmldoc->save('datas.xml');
echo ($bytes)? "Written: $bytes bytes" : 'Save failed';

//Delete node
$xmldoc = new DOMDocument('1.0', 'UTF-8');
$xmldoc->load('datas.xml');
$student = $xmldoc->getElementsByTagName('Student')->item(2);//Directly find the node to be deleted
$student->parentNode->removeChild($student);//The parent node’s Delete method
$xmldoc->save('datas.xml');

//Modify node value
$student = $xmldoc->getElementsByTagName('student')->item(2);
$student->getElementsByTagName('age')-> item(0)->nodeValue += 10;
$student->setAttribute('id', '110');
$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 yuan
Daming
< ;title>Tian Long Ba Bu


1002
< price>321 yuan
Zhang San
Swordsman

< ;book house="People's Publishing House">
1004
182 yuan
李思
Reader


*/
$xml = simplexml_load_file('books.xml');
$books = $xml->book;
echo $books[1]->title . $books[1]['house'];//directly points to the second book
foreach( $xml as $item){
echo $item->title,' ',$item['house'],'
';
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326981.htmlTechArticleCopy the code as follows: /*?xml version="1.0" encoding="UTF-8" standalone=" no"? !-- css style definition, no dots. For example: name{color:red;} -- ?xml-stylesheet type="text/css" hre...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!