Welcome to WebmasterWorld Guest from 54.158.238.108

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP & Xpath query

     
8:37 am on Jun 22, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 18, 2006
posts:46
votes: 0


I have a routine to parse xml which usually works fine but am having an issue with one particular XML file.

usually I have a structure of

<person>
<name>Joe Bloggs</name>
<age>21</age>
</person>
<person>
<name>Sarah Bloggs</name>
<age>25</age>
</person>

Which I usually parse with a string like this:

foreach ($xml->xpath('//name') as $names) {

I have another XML file which has a different format and takes the following format:


<Names>
<name>Joe Bloggs</name>
<age>21</age>
</Names>
<Names>
<name>Sarah Bloggs</name>
<age>25</age>
</Names>

The problem I've got is that because the top level is also contains the phrase names which I search for with XPath - the parsing doesn't work.

Any suggestions gratefully received

9:53 am on June 22, 2009 (gmt 0)

New User

10+ Year Member

joined:Apr 16, 2006
posts:15
votes: 0


You need to wrap your XML in a top level element.

How about something like :-

[pre]

$xml=<<<XML
<?xml version='1.0'?>
<doc>
<person>
<name>Joe Bloggs</name>
<age>21</age>
</person>
<person>
<name>Sarah Bloggs</name>
<age>25</age>
</person>
</doc>
XML;

$doc=new SimpleXMLElement($xml);

var_dump($doc->xpath('//person/name'));

[/pre]

1:08 pm on June 22, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 18, 2006
posts:46
votes: 0


Hi,

thanks for that but I can not alter the XML, it is sent to me via the web, also the XML that starts Person is fine to process - it's the one that starts Names that is the issue.

thanks again, any other ideas thoguh?
Brett

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members