Parse the php DOMElement implementation code for manipulating xml documents
/*<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- css Style definition, no dot. Such as: name{color:red;} --><?xml-stylesheet type="text/css" href="css.css"?><!-- The introduction of dtd Document definition file (root element: class) <!DOCTYPE The class SYSTEM "class.dtd" /> --><!-- <!DOCTYPE The class [<!ELEMENT The class ( students +)><!ELEMENT students ( The name , age , introduce )><!ELEMENT The name (#PCDATA)><!ELEMENT age (#PCDATA)><!ELEMENT introduce (#PCDATA)>] /> -->< The class >< students number="101">< The name > The Monkey King </ The name >< The name > Sun walkers </ The name >< age >123</ age >< introduce ><![CDATA[&*$% Special string ^&#$&]]></ introduce ></ students >< students number="10"2">< The name > Bones jing </ The name >< age >140</ age >< introduce > Introduce the content </ introduce ></ students ></ The class >*/$xmldoc = new DOMDocument('1.0', 'UTF-8');$xmldoc->load('datas.xml');$itemsNodeList = $xmldoc->getElementsbyTagName(' students ');$itemElement = $itemsNodeList->item(0);// Get the first 1 A complete student information node$itemChildsNodeList = $itemElement->getElementsbyTagName(' The name ');// Gets the child node "name," which may have multiple names$itemChildNode = $itemChildsNodeList->item(0);// Get the first 1 Name nodeecho $itemChildNode->nodeValue;// Output node value// Encapsulated as a function$nodeArr = array(' The name ', ' age ', ' introduce ');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, ' students ', $nodeArr);print_r($data);
// Add a node$xmldoc = new DOMDocument('1.0', 'UTF-8');$xmldoc->load('datas.xml');$items = $xmldoc->getElementsByTagName(' The class ')->item(0);// The root node$student = $xmldoc->createElement(' students ');// create 1 A new student node$stu_name = $xmldoc->createElement(' The name ',' zhang 3');$stu_age = $xmldoc->createElement(' age ','15');$stu_intro = $xmldoc->createElement(' introduce ',' Strong practical 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 into the : $bytes byte " : ' Save failed ';// Remove nodes$xmldoc = new DOMDocument('1.0', 'UTF-8');$xmldoc->load('datas.xml');$student = $xmldoc->getElementsByTagName(' students ')->item(2);// Go directly to the node you want to delete$student->parentNode->removeChild($student);// The delete method of the parent node$xmldoc->save('datas.xml');// Modify node values$student = $xmldoc->getElementsByTagName(' students ')->item(2);$student->getElementsByTagName(' age ')->item(0)->nodeValue += 10;$student->setAttribute('id', '110');$xmldoc->save('datas.xml');// application Xpath 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 action xml/*<?xml version="1.0" encoding="UTF-8"?><books><book house=" Tsinghua university press "><code>1001</code><price>200 yuan </price><author> daming </author><title> Draco 8 The ministry of </title></book><book house=" Peking University publishing "><code>1002</code><price>321 yuan </price><author> zhang 3</author><title> The Legendary Swordsman </title></book><book house=" people People published "><code>1004</code><price>182 yuan </price><author> li 4</author><title> The reader </title></book></books>*/$xml = simplexml_load_file('books.xml');$books = $xml->book;echo $books[1]->title . $books[1]['house'];// Direct pointing control 2 This bookforeach($xml as $item){ echo $item->title,' ',$item['house'],'<br/>';}