PHP/XML/xpath

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

Combine it with an XPath search

 
<?
    $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( );
?>



Searching and Filtering with XPath

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



Using XPath and DOM

 
$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>



Using XPath and SimpleXML in a basic example

 
<?
$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>



Using XPath with DOM in a basic example

 
<?
$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>



Using XPath with SimpleXML in a more complicated example

 
<?
$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>



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

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



XPath DOM Query

 
<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( "<li>" . $node -> textContent . "</li>" );
}
echo( "</ul>" );
?>
</body>
</html>



xpath.php

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