Dom Basics and how PHP reads XML content Operations _php tutorial

Source: Internet
Author: User

Dom Basics and how PHP reads XML content operations


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

The code is as follows:




Joe
Woman
20


Zhou yu
Man
25


Class.xml corresponding DOM tree structure diagram

PHP Files (Operations on XML documents)

Query Operation Case:

The code is as follows:

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:

The code is as follows:

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;
?>

http://www.bkjia.com/PHPjc/947790.html www.bkjia.com true http://www.bkjia.com/PHPjc/947790.html techarticle Dom Basics and PHP methods for reading XML content operations Dom (Document Object model) The core idea is to think of an XML file as an object model and then pass the object's ...

  • 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.