Page 1 of 1

Extract Multiple Entries with XML

Posted: Wed Mar 17, 2010 11:40 am
by timWebUK
Hi,

I recently posted this code in another thread to help someone, then I found myself intrigued as to how to access multiple entries within an XML file:

Code: Select all

<?php
//HEREDOC containing XML
$xmlAddressBook = <<<XML
<?xml version='1.0'?> 
<addressbook>
    <person id="1">
        <name>John Smith</name>
        <phone>01232458987</phone>
        <address>20 Quiet Lane</address>
    </person>
    <person id="2">
        <name>John Jackson</name>
        <phone>01234458657</phone>
        <address>20 Loud Lane</address>
    </person>
</addressbook>
XML;
 
//Load XML heredoc into a variable using simplexml_load_string
$getXML = simplexml_load_string($xmlAddressBook);
 
print_r($getXML);
I'm wondering how can I print elements from <person id="n">? How do I specify?

Cheers

Re: Extract Multiple Entries with XML

Posted: Wed Mar 17, 2010 2:13 pm
by Sofw_Arch_Dev
Sounds like a perfect job for XML XPath. Very handy stuff. Check out PHP's xpath support at http://php.net/manual/en/simplexmlelement.xpath.php

Re: Extract Multiple Entries with XML

Posted: Thu Mar 18, 2010 4:52 am
by timWebUK
Thanks, I'll look into that as well. However, is there no way of referencing the ID=1 or 2 in my example?

The array seems to be constructed like this:

Code: Select all

SimpleXMLElement Object 
( 
    [person] => Array   
    ( 
        [0] => SimpleXMLElement Object 
        ( 
            [@attributes] => Array 
            ( 
                [id] => 1 
            ) 
                [name] => John Smith
                [phone] => 01232458987 
                [address] => 20 Quiet Lane 
        )   
        
        [1] => SimpleXMLElement Object 
        ( 
            [@attributes] => Array 
            ( 
                [id] => 2 
            ) 
                [name] => John Jackson 
                [phone] => 01234458657 
                [address] => 20 Loud Lane 
        )
    ) 
)

Re: Extract Multiple Entries with XML

Posted: Thu Mar 18, 2010 12:27 pm
by Sofw_Arch_Dev
What have you tried with respect to XPath?

Re: Extract Multiple Entries with XML

Posted: Fri Mar 19, 2010 4:52 am
by timWebUK
Well I find out I don't have that class installed! But it does look quite good, I am just curious about the way I've done it because you can just access the different elements within the array to access the different XML pieces?

Re: Extract Multiple Entries with XML

Posted: Fri Mar 19, 2010 12:44 pm
by Sofw_Arch_Dev
No XPath support. That's a bummer. XPath would allow you to do this:

Code: Select all

 
  $matchedElementsArray = $mySimpleXMLElement->xpath( "//someElement[@someAttribute='someValue']" );
 
In your case

Code: Select all

 
   $matchedElementsArray = $mySimpleXMLElement->xpath( "//person[@id='1']" );
 
I don't suppose it's an option to remake PHP eh?

Re: Extract Multiple Entries with XML

Posted: Sat Mar 20, 2010 9:28 am
by timWebUK
Ah that's so simple, I have two areas where I can upload to, I believe one of them has XPath support, so I'll try on that. Cheers!