PHP uses DOM and simplexml to read XML documents in a detailed way

Source: Internet
Author: User
This article mainly introduces PHP using DOM and simplexml to read XML document method, combined with instance form analysis PHP using DOM and simplxml for XML file creation, loading, reading and other related operations skills, the need for friends can refer to the next

This example describes how PHP uses the DOM and simplexml to read XML documents. Share to everyone for your reference, as follows:

The example uses the DOM to get the title of all Jin Yong novels in the following XML document, where the XML document is located./books.xml:

<?xml version= "1.0" encoding= "Utf-8"?><root> <book>  <title> tianlong eight parts </title>  <author> Jin Yong </author> </book> <book>  <title> clan </title>  <author> Cologne </author> </book> <book>  <title> Sky slayer Dragon Kee </title>  <author> Jin Yong </ author> </book> <book>  <title> journey </title>  <author> Wu Chengen </author> </book> <book>  <title> Condor hero </title>  <author> Jin Yong </author> </book> <book>  <title> Condor Heroes </title>  <author> Jin Yong </author> </book></root >

Implemented with DOM code:

The DOM reads the XML document step: 1, creates the DOM object--"2, loads the DOM document content--" 3, intercepts to read the content the label--"obtains to read the content.

Header (' Content-type:text/html;charset=utf-8 '), $arr =array (), $dom = new DOMDocument ();//Create DOM Object $dom->load ('./ Books.xml ');//Load XML document PRINT_R ($dom); Echo ' 

Objects are returned after using getElementsByTagName and childNodes, so they must be followed by item (int), even if the object they return contains only one item and must be specified with item (0), otherwise an error occurs.

Implemented with SimpleXML code:

$simxml = Simplexml_load_file ('./books.xml '); $t = $simxml->book; $arr =array (); foreach ($t as $v) {if ($v->author== ' Jin Yong ') {  $arr [] = (string) $v->title;}} Print_r ($arr);

The object is returned after using Simplexml_load_file, and the items in the object have both an object and an array, whether it is an object or an array, and the contents of the loop can be used with foreach. The last thing the instance gets is $v->title is actually an object, so it's going to be converted to a string using string.

The above is the whole content of this article, I hope that everyone's study has helped.


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.