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
timWebUK
Forum Contributor
Posts: 239 Joined: Thu Oct 29, 2009 6:48 am
Location: UK
Post
by timWebUK » Wed Mar 17, 2010 11:40 am
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
timWebUK
Forum Contributor
Posts: 239 Joined: Thu Oct 29, 2009 6:48 am
Location: UK
Post
by timWebUK » Thu Mar 18, 2010 4:52 am
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
)
)
)
Sofw_Arch_Dev
Forum Commoner
Posts: 60 Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US
Post
by Sofw_Arch_Dev » Thu Mar 18, 2010 12:27 pm
What have you tried with respect to XPath?
timWebUK
Forum Contributor
Posts: 239 Joined: Thu Oct 29, 2009 6:48 am
Location: UK
Post
by timWebUK » Fri Mar 19, 2010 4:52 am
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?
Sofw_Arch_Dev
Forum Commoner
Posts: 60 Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US
Post
by Sofw_Arch_Dev » Fri Mar 19, 2010 12:44 pm
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?
timWebUK
Forum Contributor
Posts: 239 Joined: Thu Oct 29, 2009 6:48 am
Location: UK
Post
by timWebUK » Sat Mar 20, 2010 9:28 am
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!