PHP/XML/xpath

Материал из Web эксперт
Перейти к: навигация, поиск

Combine it with an XPath search

   <source lang="html4strict">

<?

   $xml = simplexml_load_file("employees.xml");
   echo $xml->asXML( );
   $xml->employee[1]->age = 55;
   $employees = $xml->xpath("/employees/employee[name="A"]");
   $employees[0]->title = "Officer";
   echo $xml->asXML( );

?>

 </source>
   
  


Searching and Filtering with XPath

   <source lang="html4strict">

<?

   $xml = simplexml_load_file("employees.xml");
   $names = $xml->xpath("/employees/employee/name");
   foreach($names as $name) {
           echo "Found $name
"; } echo "
"; $employees = $xml->xpath("/employees/employee"); foreach($employees as $employee) { echo "Found {$employee->name}
"; } $names = $xml->xpath("//name"); foreach($names as $name) { echo "Found $name
"; }

?>

 </source>
   
  


Using XPath and DOM

   <source lang="html4strict">

$dom = new DOMDocument; $dom->load("address-book.xml"); $xpath = new DOMXPath($dom); $email = $xpath->query("/address-book/person/email"); // <?xml version="1.0"?> <address-book>

   <person id="1">
       <firstname>D</firstname>
       <lastname>S</lastname>
       <city>New York</city>
       <state>NY</state>
       <email>s@php.net</email>
   </person>
   <person id="2">
       <firstname>A</firstname>
       <lastname>T</lastname>
       <city>San Francisco</city>
       <state>CA</state>
       <email>a@php.net</email>
   </person>

</address-book>

 </source>
   
  


Using XPath and SimpleXML in a basic example

   <source lang="html4strict">

<? $s = simplexml_load_file("address-book.xml"); $emails = $s->xpath("/address-book/person/email"); foreach ($emails as $email) {

   // do something with $email

} ?> // <?xml version="1.0"?> <address-book>

   <person id="1">
       <firstname>D</firstname>
       <lastname>S</lastname>
       <city>New York</city>
       <state>NY</state>
       <email>s@php.net</email>
   </person>
   <person id="2">
       <firstname>A</firstname>
       <lastname>T</lastname>
       <city>San Francisco</city>
       <state>CA</state>
       <email>a@php.net</email>
   </person>

</address-book>

 </source>
   
  


Using XPath with DOM in a basic example

   <source lang="html4strict">

<? $dom = new DOMDocument; $dom->load("address-book.xml"); $xpath = new DOMXPath($dom); $emails = $xpath->query("/address-book/person/email"); foreach ($emails as $e) {

   $email = $e->firstChild->nodeValue;
   // do something with $email

} ?> // <?xml version="1.0"?> <address-book>

   <person id="1">
       <firstname>D</firstname>
       <lastname>S</lastname>
       <city>New York</city>
       <state>NY</state>
       <email>s@php.net</email>
   </person>
   <person id="2">
       <firstname>A</firstname>
       <lastname>T</lastname>
       <city>San Francisco</city>
       <state>CA</state>
       <email>a@php.net</email>
   </person>

</address-book>

 </source>
   
  


Using XPath with SimpleXML in a more complicated example

   <source lang="html4strict">

<? $s = simplexml_load_file("address-book.xml"); $people = $s->xpath("/address-book/person"); foreach($people as $p) {

   list($firstname) = $p->xpath("firstname");
   list($lastname) = $p->xpath("lastname");
   
   print "$firstname $lastname\n";

} ?> // <?xml version="1.0"?> <address-book>

   <person id="1">
       <firstname>D</firstname>
       <lastname>S</lastname>
       <city>New York</city>
       <state>NY</state>
       <email>s@php.net</email>
   </person>
   <person id="2">
       <firstname>A</firstname>
       <lastname>T</lastname>
       <city>San Francisco</city>
       <state>CA</state>
       <email>a@php.net</email>
   </person>

</address-book>

 </source>
   
  


XPath can be used to filter your results according to any values

   <source lang="html4strict">

<?

   $xml = simplexml_load_file("employees.xml");
   $employees = $xml->xpath("/employees/employee[name="Laura Pollard"]");
   foreach($employees as $employee) {
           echo "Found {$employee->name}
"; } $employees = $xml->xpath("/employees/employee[age<54]"); foreach($employees as $employee) { echo "Found {$employee->name}
"; } $employees = $xml->xpath("//employee[age>=48]"); foreach($employees as $employee) { echo "Found {$employee->name}
"; }

?>

 </source>
   
  


XPath DOM Query

   <source lang="html4strict">

<html> <head>

<title>XPath DOM Query</title>

</head> <body>

<?php $dom = new DomDocument(); $dom -> load( "food.xml" ); $xp = new DomXPath( $dom ); $fruits = $xp -> query( "/food/fruit/item" ); foreach($fruits as $node) {

echo( "
  • " . $node -> textContent . "
  • " );

    } echo( "</ul>" ); ?> </body> </html>

     </source>
       
      
    


    xpath.php

       <source lang="html4strict">
    
    

    <?php

      $xml = simplexml_load_file("books.xml");
      $authors = $xml->xpath("/library/book/author");
      foreach($authors AS $author) {
         echo "$author
    "; }

    ?>

     </source>