| 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
|
|
|