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 -jb51.net </name> <website> http://www.bkjia.com </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> http://www.bkjia.com </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:
Http://tools.jb51.net/code/xmljson
Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat
XMLOnline compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress
XMLCode Online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat