PHP Parse XML Instance tutorial using XPath

Source: Internet
Author: User
Tags foreach array php tutorial php web development php website query xml xpath xpath

XML files are widely used in lightweight applications developed in PHP, and PHP parses and reads XML files in a variety of ways, such as JS DOM, SIMPLEXML, XPath, and so on, parsing XML files in PHP, and today is about using XPath to parse an instance of XML It also introduces some of the basic XPath syntax through XPath parsing an instance of XML.

  What is XPath?

Xapth is primarily used to query information in XML documents by using path expressions to parse XML files and read data in XML files.

  PHP Parsing XML Recommendation tutorial

How to parse XML documents under PHP4 and PHP5 versions

  XML document that needs to be parsed by PHP

1
2
3
4
5
6
7
8
9
Ten
One





"
"
[
]

[
]
<?xml version= "1.0" encoding= "GBK"
<LeapsoulXML>  
    <LeapsoulInfo>  
        <name>php website Development-php Tutorial-leapsoul.cn</ Name>
        <WEBSITE>HTTP://WWW.LEAPSOUL.CN</WEBSITE>
    </ Leapsoulinfo>

    <LeapsoulInfo>  
        <NAME>
&nbs P         <KEYWORDS>
          <keyword keyid= ' 1 ' >php website development </ Keyword>
          <keyword keyid= ' 2 ' >php tutorial </keyword>
    & nbsp     <keyword keyid= ' 3 ' >php</keyword>
      &NBSp   </KEYWORDS>
          <SEO>SEO</SEO>
      &NBSP ; </name>
    </leapsoulinfo>

    <WebInfo>  
    &NB Sp   <name>php website Development </name>
        <website>http://www.leapsoul.cn</ Website>
    </webinfo>
</leapsoulxml>

  XPath Grammar tutorial

  / representation begins parsing from the root node in the XML file

  // represents matching the selected current node in an XML file without regard to its positional relationship (similar to a fuzzy query in SQL)

  . Indicates that the current node is selected

  .. Indicates that the parent node of the current node is selected

  @ indicates matching specific node or attribute

  PHP Parse XML Instance first step

1
$xml = simplexml_load_file (' Leapsoulcn.xml ');

description : Use PHP simplexml to load XML documents that need parsing, simplexml default PHP5 support.

  PHP Parse XML XPath instance one:

1
2
3
foreach ($xml->xpath (' Leapsoulinfo ') as $value) {
Print_r ($value);
}

Note : In this XPath instance, the XPath query specifies the node, returns all of its subnodes as an array, and prints out the final array structure through the PHP print_r function.

  PHP Parse XML XPath instance two

1
2
3
foreach ($xml->xpath (' Leapsoulinfo/name ') as $value) {
Print_r ($value);
}

Note : In this XPath instance, you specify a specific child node of the Xapth query in the form of an XPath path expression that returns the element of the child node as an array.

  PHP Parse XML XPath instance three

1
2
3
foreach ($xml->xpath ('//name ') as $value) {
Print_r ($value);
}

Note : In this XPath instance, the element that uses XPath to query all name nodes in an XML file is distinguished from the previous XPath instance because name has no location relationship.

  PHP Parse XML XPath instance four

1
2
3
foreach ($xml->xpath (' Leapsoulinfo//name ') as $value) {
Print_r ($value);
}

description : php resolves a child node under a specified node in an XML file, returns all its child nodes as an array, ignoring the location of the specified child node in the Leapsoulinfoxml.

  PHP Parse XML XPath instance five

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

description : Finds keyid=1 elements in an XML file that requires PHP parsing, and returns as an array.

OK, the above several PHP XPath instance tutorials are the most basic method of parsing XML files in PHP, and can be used to make complex XML queries and parsing by using XPath path expressions rationally, which is as convenient as SQL.

  Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.







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.