Jump to content

Trouble with XML namespaces in web service responses using $DOM...???


Recommended Posts

I'm getting a little lost with namespaces in my XML responses from API's.  I'm using DOM (DOMXPath) to parse the data out of it and before that works I have to either register the namespace in DOMXPath or strip the XML response of all the namespace information prior to loading it into DOM.  I'd rather do it the correct way and use namespaces.

 

Here's the thing.  Consider the following XML: http://www.angelleye.com/phpTraining/XML_Parsing_Samples/fmresult.xml

 

That has the xmlns="http://www.filemaker.com/xml/fmresultset" associated with it.  As such, when I load this into DOM I have the following:

 

$xmlDom = new DOMDocument();
$xmlDom -> loadXML($xmlResponse);
$xmlDomXPath = new DOMXPath($xmlDom);
$xmlDomXPath -> registerNamespace("fm", "http://www.filemaker.com/xml/fmresultset");
$records = $xmlDomXPath -> query("/fm:fmresultset/fm:resultset/fm:record");
$recordCount = $records -> length;
//etc.

 

That works great.  I just add fm: to the front of all the XPath elements from there on and it works just fine.  Now consider the following XML response: http://angelleye.com/TodaysTransactions.xml

 

That thing is has a whole bunch of xmlns additions to it.  currently I'm trying to use XPath just like the example above but I haven't added the registernamepsace stuff yet so it's simply not finding anything with the XPath queries.  Do I have to add the registernamespace method for every single one of those?  And if so, how do I know what prefix to use when parsing the XML?

 

It works just fine without doing anything with namepsaces if I just use $DOM -> getElementByTagName() but there are lots of times when I need to be able to use XPath...or even just prefer it.  I've tried looking into PHP SOAP (www.php.net/soap) but that and anything else I see seems to be more about creating your own web services than simply parsing the data out of the response.

 

Any information on this would be greatly appreciated.  Thanks!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.