homepage Welcome to WebmasterWorld Guest from 54.226.173.169
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP & Xpath query
breham




msg:3938064
 8:37 am on Jun 22, 2009 (gmt 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

 

azazello




msg:3938088
 9:53 am on Jun 22, 2009 (gmt 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]

breham




msg:3938192
 1:08 pm on Jun 22, 2009 (gmt 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved