• Hi.
    I’m trying to extract a data(phone number) from XML file using PHP code.

    Here is my XML (pretty simple)
    <?xml version=”1.0″ encoding=”UTF-8″?>
    <list>
    <city>oakland</city>
    <phone>510.444.6500</phone>

    <city>fremont</city>
    <phone>510.792.9410</phone>

    <city>hayward</city>
    <phone>510.792.9410</phone>
    </list>

    I want to extract phone number by page title.
    city name is page title.

    Here is some code what I’m did.. but did not work..

    <?php
    $xml = new DOMDocument();
    $xml->loadXML(‘phone-list.xml’);
    $city = $xml->getElementsByTagName(‘city’);
    $phone = $xml->getElementsByTagName(‘phone’);
    $title = get_the_title();
    if ($title == $city) {
    echo $phone;
    }
    ?>

    Any idea??

    Thanks!

  • The topic ‘Need PHP code to extract data from XML’ is closed to new replies.