php Crawl XML phrases

Discussion in 'PHP' started by vOlLvEriNe, Sep 10, 2014.

  1. #1
    I Have This Xml File
    <?xml version="1.0" encoding="UTF-8"?>
    <library>
        <book id="1">
            <title>PHP and MySQL</title>
            <author id="test">Miguel Alvarez</author>
            <publisher>WebHole</publisher>
            <price>1.99</price>
        </book>
        <book id="2">
            <title>JAVA 123</title>
            <author id="test2">WIlliam Vega</author>
            <publisher>WebHole</publisher>
            <price>2.99</price>
        </book>
    </library>
    Code (markup):
    And This PHP file
     <?php
    $url = "test.xml";
    $xml = simplexml_load_file($url);
    foreach ($xml->book as $entry){
    echo $entry->author . ' <br />';
    }
    ?>
    PHP:
    It'll show
    Miguel Alvarez
    WIlliam Vega


    how can I get only Miguel Alvarez as result, Like foreach ($xml->book['id=1'] as $entry)
    Or echo $entry->author['id=test'] . ' <br />';

    Please help me
     
    vOlLvEriNe, Sep 10, 2014 IP
  2. Krellen

    Krellen Greenhorn

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #2
    You probably want to look at using SimpleXMLElement::xpath to accomplish that.

    http://php.net/manual/en/simplexmlelement.xpath.php

    Or you could always just stick some if() logic inside your foreach().
     
    Krellen, Sep 10, 2014 IP
  3. pmf123

    pmf123 Notable Member

    Messages:
    1,449
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    215
    #3
    you could put an extra value into each book like an ISBN and then use

    if ($entry->isbn == '1')
    {
    }
     
    pmf123, Sep 17, 2014 IP
  4. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #4
    For xml / html parsing with PHP - 'QueryPath' is your best bet.
     
    kutchbhi, Sep 18, 2014 IP