DOM basics and php methods for reading xml content _ PHP Tutorial

Source: Internet
Author: User
DOM basics and php methods for reading xml content. DOM basics and php methods for reading xml content DOM (DocumentObjectModel): Document Object Model. The core idea is to regard an xml file as an object model, and then read xml content through the DOM of the object and php.

DOM (Document Object Model): Document Object Model. The core idea is to regard xml files as an object model and then operate xml files through objects.

Php performs addition, deletion, modification, and query (curd) Operations on xml documents. the specific analysis is as follows:

Xml document: class. xml

The code is as follows:




Xiaoqiao
Female
20


Zhou Yu
Male
25


DOM tree structure corresponding to class. xml

Php file (operations on xml files)

Query operation case:

The code is as follows:

// 1. create a DOMDocument object. This object indicates an xml file.
$ Xmldoc = new DOMDocument ();
// 2. load the xml file (specify the xml file to be parsed, and the dom tree node will be loaded to the memory)
$ Xmldoc-> load ("class. xml ");
// 3. goal: Get the name of the first student
// Step 1 of 3.1: Read all students
$ Students = $ xmldoc-> getElementsByTagName ("student"); // method getElementsByTagName: find the corresponding node based on the name of the given node (student here, the returned DOMNodeList object is equivalent to retrieving all the students. You can use var_dump ($ students) to view and find the manual based on the returned values, and view the following attributes and methods.
Echo "total". $ students-> length. "students
";

// Read the first student 3.2
$ Stu1 = $ students-> item (0); // read to the first student. The return value is a DOMElement object. Directly echo $ stu1-> nodeValue; the name, sex, and age are all output.
// 3.3 retrieve the name of the first student
$ Stu1_name = $ stu1-> getElementsByTagName ("name ");
// 3.4 read the name
Echo $ stu1_name-> item (0)-> nodeValue;
?>

Note:

(1) coding problems;
(2) here is only a basic demonstration, which is troublesome and will be followed by loops and functions;
(3) use var_dump () to check the return value of the variable, and then find the attributes and methods in the returned value in the manual.
(4) in the whole order, getElementByTagName () does not need to be read at a layer. In fact, it can be read directly to the node name without first reading student (of course, if there are multiple names under the same student, there will be problems. here we need to learn new knowledge points xpath ).

So the above code can be changed:

The code is as follows:

// 1. create a DOMDocument object. This object indicates an xml file.
$ Xmldoc = new DOMDocument ();
// 2. load the xml file (specify the xml file to be parsed, and the dom tree node will be loaded to the memory)
$ Xmldoc-> load ("class. xml ");
// 3. goal: Get the name of the first student
$ Stu = $ xmldoc-> getElementsByTagName ("name"); // locate the node name directly.
$ Stu1 = $ stu-> item (0); // When item (1), you can get Zhou Yu
Echo $ stu1-> nodeValue;
?>

Lateral DOM (Document Object Model): Document Object Model. The core idea is to regard xml files as an object model, and then use 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.