Dom Basics and PHP read XML content manipulation methods, Domxml
This paper describes the DOM Foundation and how PHP reads XML content operations. Share to everyone for your reference. The specific analysis is as follows:
DOM: Document Object model. The core idea is to think of an XML file as an object model, and then manipulate the XML file through the object's way.
PHP to the XML document for the curd operation, the specific analysis is as follows:
XML Document: Class.xml
Copy the Code code as follows: <?xml version= "1.0" encoding= "Utf-8"?>
Joe
Woman
20
Zhou yu
Man
25
Class.xml corresponding DOM tree structure diagram
PHP Files (Operations on XML documents)
Query Operation Case:
Copy the Code code as follows: <?php
1. Create a DOMDocument object. The object represents the XML file
$xmldoc = new DOMDocument ();
2. Load the XML file (specifies which XML file to parse, at which point the DOM tree node is loaded into memory)
$xmldoc->load ("Class.xml");
3. Goal: Get the first Student's name
3.1 First step, read all the students
$students = $xmldoc->getelementsbytagname ("student");//Method getElementsByTagName: Finds the corresponding node based on the given node name (here is student), and returns An object of type Domnodelist, which is equivalent to removing all students. You can view the Var_dump ($students) and find the manual based on the return value to see the properties and methods below.
echo "Shared". $students->length. " A student
";
3.2 Reading of the first student
$stu 1 = $students->item (0);//Read to the first student. The return value is the DomElement object. The direct echo $stu 1->nodevalue; the name,sex,age output.
3.3 Take out the first student's name
$stu 1_name = $stu 1->getelementsbytagname ("name");
3.4 Read the name
echo $stu 1_name->item (0)->nodevalue;
?>
Note the point:
(1) Coding problem;
(2) This is just a basic demonstration, more trouble, the back of the use of loops and functions to operate;
(3) Use Var_dump () to see what the return value of the variable is, and then find the property and method under that return value in the manual based on the return value.
(4) The whole sequence down, getelementbytagname () does not need a layer of read, in fact, can be read directly to the node name, and do not need to read student first (of course, if the same student, there are multiple name, there will be problems, Here you need to learn a new point of knowledge XPath).
So the above code can be changed simply:
Copy the Code code as follows: <?php
1. Create a DOMDocument object. The object represents the XML file
$xmldoc = new DOMDocument ();
2. Load the XML file (specifies which XML file to parse, at which point the DOM tree node is loaded into memory)
$xmldoc->load ("Class.xml");
3. Goal: Get the first Student's name
$stu = $xmldoc->getelementsbytagname ("name");//Find node directly name
$stu 1 = $stu->item (0);//item (1), can be taken to Zhou Yu
Echo $stu 1->nodevalue;
?>
I hope this article is helpful to everyone's PHP operation XML programming.
http://www.bkjia.com/PHPjc/946738.html www.bkjia.com true http://www.bkjia.com/PHPjc/946738.html techarticle Dom Base and PHP read XML content operation method, Domxml This article describes the DOM Foundation and PHP read XML content operation method. Share to everyone for your reference. The specific analysis is as follows: D ...