Welcome to WebmasterWorld Guest from 54.167.0.111

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP & Xpath query

     

breham

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

5+ Year Member



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

azazello

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

5+ Year Member



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]

breham

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

5+ Year Member



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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month