Tutorial on parsing XML instances using xpath in PHP

Source: Internet
Author: User
Tags xml xpath
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.

Related Article

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.