PHP uses xpath to parse XML, and xpath to parse xml

Source: Internet
Author: User
Tags php website

PHP uses xpath to parse XML, and xpath to parse xml

This example describes how PHP parses XML using xpath. We will share this with you for your reference. The details are as follows:

XML files are widely used in lightweight applications developed by PHP websites. There are many ways for PHP to parse and read XML files, such as js dom, SimpleXml, and Xpath to parse XML files, let's talk about how to use Xpath to parse XML instances in PHP today, and introduce some basic Xpath syntaxes through Xpath to parse XML instances.

What is Xpath?

Xapth is mainly used to query information in XML documents. You can use path expressions to parse XML files and read data in XML files.

PHP parsing XML recommended Tutorial:How to parse XML documents in PHP4 and PHP5 versions

XML documents that require PHP parsing:

<? Xmlversion = "1.0" encoding = "gbk"?> <LeapsoulXML> <LeapsoulInfo> <name> PHP website development-PHP tutorial </name> <website> </website> </LeapsoulInfo> <name> <keywords> <keywordkeyid = '1'> PHP website development </keyword> <keywordkeyid = '2'> PHP tutorial </keyword> <keywordkeyid = '3'> PHP </keyword> </keywords> <seo> SEO </seo> </name> </LeapsoulInfo> <WebInfo> <name> PHP website development </name> <website> </website> </WebInfo> </LeapsoulXML>

Xpath syntax tutorial

/Indicates parsing starts from the root node in the XML file
// Match the selected current node in the XML file without considering its location relationship (similar to fuzzy query in SQL)
. Indicates selecting the current node
.. Indicates selecting the parent node of the current node
@ Indicates matching a specific node or attribute

Step 1 of PHP parsing XML instances

$xml = simplexml_load_file('leapsoulcn.xml');

Note: Use PHP SimpleXML to load the XML document to be parsed. SimpleXML supports PHP5 by default.

Instance 1:

foreach($xml->xpath('LeapsoulInfo')as $value) {   print_r($value);}

Note: In this Xpath instance, you can query a specified node through Xpath, return all its subnodes in the form of arrays, and print the final array structure through the print_r function of PHP.

Example 2:

foreach($xml->xpath('LeapsoulInfo/name')as $value) {   print_r($value);}

Note: In this Xpath instance, you can use the Xpath path expression to specify a special subnode that requires Xapth query and return the elements of this subnode in an array.

Example 3:

foreach($xml->xpath('//name')as $value) {   print_r($value);}

Note: In this Xpath instance, you can use // to query the elements of all name nodes in the XML file using Xpath. the difference with the previous Xpath instance is that name has no positional relationship.

Example 4:

foreach($xml->xpath('LeapsoulInfo//name')as $value) {   print_r($value);}

Note: PHP parses a subnode under a specified node in the XML file and returns all its subnodes in the form of arrays. This ignores the positional relationship between the specified subnode in LeapsoulInfoXML.

Example 5:

foreach($xml->xpath("//name/keywords/keyword[@keyid='1']")as $value) {   print_r($value);}

Note:In the XML file to be parsed by PHP, use Xpath to find the elements with keyid = 1 and return them as arrays.

OK. The above PHP Xpath instance tutorials are the most basic method for PHP to parse XML files. You can use the Xpath path expressions to perform complex XML queries and resolutions, it feels as convenient as SQL.

PS: Here are some online tools for xml operations for your reference:

Online XML/JSON conversion tools:

Online formatting XML/online compression XML:

XMLOnline compression/formatting tools:

XMLCode Online formatting and beautification tools:

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