XML files are widely used in lightweight applications developed by PHP websites. There are many ways for PHP to parse and read XML files, for example, JSDOM and SimpleXml files are widely used in lightweight applications developed on the PHP website. There are many ways for PHP to parse and read XML files, for example, js dom, SimpleXml, Xpath, and other methods to parse XML files. let's talk about the example of using Xpath to parse XML in PHP today, at the same time, we use Xpath to parse XML instances to introduce some basic Xpath syntax.
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 recommendation tutorial
How to parse XML documents in PHP4 and PHP5 versions
XML file to be parsed by PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
PHP website development-PHP Tutorial -Leapsoul.cn Http://www.leapsoul.cn PHP website development PHP Tutorial PHP SEO PHP website development Http://www.leapsoul.cn |
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
..Select the parent node of the current node
@Indicates matching a specific node or attribute
Step 1 of PHP parsing XML instances
1
|
$ Xml = simplexml_load_file ('leapsoulcn. XML '); |
Description: Use PHP SimpleXML to load the XML document to be parsed. SimpleXML is supported by PHP5 by default.
PHP parses XML Xpath instance 1:
1 2 3
|
Foreach ($ xml-> xpath ('leapsoulinfo') as $ value ){ Print_r ($ value ); } |
Description: In this Xpath instance, query the 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.
PHP parsing XML Xpath Example 2
1 2 3
|
Foreach ($ xml-> xpath ('leapsoulinfo/name') as $ value ){ Print_r ($ value ); } |
Description: In this Xpath instance, specify a specific child node that requires Xapth query using the Xpath path expression, and return the element of this child node in the form of an array.
PHP parsing XML Xpath instance 3
1 2 3
|
Foreach ($ xml-> xpath ('// name') as $ value ){ Print_r ($ value ); } |
Description: In this Xpath instance, the element of all name nodes in the XML file is queried using Xpath using //. The difference from the previous Xpath instance is that name has no positional relationship.
PHP parsing XML Xpath Example 4
1 2 3
|
Foreach ($ xml-> xpath ('leapsoulinfo // name') as $ value ){ Print_r ($ value ); } |
Description: PHP parses a subnode under a specified node in the XML file and returns all its subnodes in the form of arrays, ignoring the location relationship between the specified subnode in LeapsoulInfoXML.
PHP parsing XML Xpath instance 5
1 2 3
|
Foreach ($ xml-> xpath ("// name/keywords/keyword [@ keyid = '1']") as $ value ){ Print_r ($ value ); } |
Description: Search for the keyid = 1 element in the XML file to be parsed by PHP using Xpath and return it as an array.
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.