Extract Multiple Entries with XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Extract Multiple Entries with XML

Post 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
User avatar
Sofw_Arch_Dev
Forum Commoner
Posts: 60
Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US

Re: Extract Multiple Entries with XML

Post 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
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Extract Multiple Entries with XML

Post 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 
        )
    ) 
)
User avatar
Sofw_Arch_Dev
Forum Commoner
Posts: 60
Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US

Re: Extract Multiple Entries with XML

Post by Sofw_Arch_Dev »

What have you tried with respect to XPath?
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Extract Multiple Entries with XML

Post 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?
User avatar
Sofw_Arch_Dev
Forum Commoner
Posts: 60
Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US

Re: Extract Multiple Entries with XML

Post 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?
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Extract Multiple Entries with XML

Post 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!
Post Reply