AndySX Posted February 1, 2011 Share Posted February 1, 2011 Hi, I've been tearing my hair out over this problem for hours (and I've precious little left as it is). It's hopefully just a 'wood for the trees' moment. I'm trying to parse XML search results using DOMDocument and DOMXPath, the problem is that the DOMDocument never seems to get populated. I can load the XML result set via cURL fine, and the XML validates ok. Here's my code: $xmldoc = new DOMDocument(); $xmldoc->load( 'http://searchserver/xml.cgi?collection=test&query=' ); $xpath = new DOMXPath( $xmldoc ); $results = $xpath->query( "/padre_result_packet" ); echo '<pre>'; print_r( $results ); echo '</pre>'; And here's an example of the XML I'm working with: <?xml version="1.0" encoding="iso-8859-1" standalone="no" ?> <PADRE_result_packet> <details> <padre_version>Unknown</padre_version> </details> <error> <usermsg>Please specify a valid query</usermsg> <adminmsg>The query parameter is invalid or missing</adminmsg> </error> </PADRE_result_packet> The result is: DOMNodeList Object ( ) Does anybody have some advice? I was getting the following error when using $xmldoc->loadXML: Warning: DOMDocument::loadXML(): Start tag expected, '<' not found in Entity, line: 1 Quote Link to comment https://forums.phpfreaks.com/topic/226366-loadxmldomxpath-problem/ Share on other sites More sharing options...
requinix Posted February 1, 2011 Share Posted February 1, 2011 XPath expressions are case-sensitive... Quote Link to comment https://forums.phpfreaks.com/topic/226366-loadxmldomxpath-problem/#findComment-1168453 Share on other sites More sharing options...
AndySX Posted February 2, 2011 Author Share Posted February 2, 2011 Thanks for the response, could have slapped myself when I read your reply last night. Unfortunately, changing the query to the correct case didn't change anything. I still got and empty object. I have since tried stepping backwards and querying the root: $results = $xpath->query( "/" ); This returns an empty object too. I don't think XPath is my problem now as the DOMDocument object is empty as well: $xmldoc = new DOMDocument(); $xmldoc->load( 'http://searchserver/xml.cgi?collection=test&query=' ); echo '<pre>'; print_r( $xmldoc ); echo '</pre>'; This code returns: DOMDocument Object ( ) Anybody got an idea as to why the DOMDocument object won't load my valid XML when cURL has no problem getting the content? Quote Link to comment https://forums.phpfreaks.com/topic/226366-loadxmldomxpath-problem/#findComment-1168755 Share on other sites More sharing options...
salathe Posted February 2, 2011 Share Posted February 2, 2011 Don't be too worried about the DOMDocument and DOMXPath objects appearing "empty" with print_r() and var_dump()... that's just how they roll. To know whether the XML was loaded, the load() method would return FALSE on failure and usually a whole heap of E_WARNINGs would be emitted. To check and see what was loaded, call the document's saveXML() method. To see if there was an error with the XPath query, the query() method would return FALSE and again E_WARNING messages will be issued (remember to turn up your error reporting!). If the XPath query was OK and you want to check how many (if any) nodes were matched, then examine the length property on the DOMNodeList returned from query(). Quote Link to comment https://forums.phpfreaks.com/topic/226366-loadxmldomxpath-problem/#findComment-1168757 Share on other sites More sharing options...
AndySX Posted February 2, 2011 Author Share Posted February 2, 2011 Thanks for the prompt response salathe. So all this time I've thought print_r() is my friend? And now I find it's been telling me lies... I've tried the following instead: echo '<pre>'; print_r( $xmldoc->saveXML() ); echo '</pre>'; And this is the strange result it's giving me: Unknown Please specify a valid query The query parameter is invalid or missing Changing my code slightly: $xmldoc = new DOMDocument(); $foo = $xmldoc->load( 'http://searchserver/xml.cgi?collection=test&query=' ); $xpath = new DOMXPath( $xmldoc ); $results = $xpath->query( "/padre_result_packet" ); echo '<pre>'; echo "foo: $foo\n"; echo "length: "; print_r( $results->length ); echo "\nDOMXPath:\n"; print_r( $xmldoc->saveXML() ); echo '</pre>'; This gives me the following: foo: 1 length: 1 DOMXPath: Unknown Please specify a valid query The query parameter is invalid or missing So... the XPath query is returning a collection consisting of 1 DOMElement (I looped through $results and print_r( $item->tagName ); is giving me PADRE_result_packet). But saveXML is failing??? I'll worry about the funky saveXML error later. I've now got something to work on. Thanks for all your help and patience Quote Link to comment https://forums.phpfreaks.com/topic/226366-loadxmldomxpath-problem/#findComment-1168764 Share on other sites More sharing options...
AndySX Posted February 2, 2011 Author Share Posted February 2, 2011 Just a note for anybody like me thinking they're clever using the Copy XPath command in firebug, firebug seems to convert all the XML to lowercase meaning the copied path may be (and in my case, definately is) completely useless. Quote Link to comment https://forums.phpfreaks.com/topic/226366-loadxmldomxpath-problem/#findComment-1168772 Share on other sites More sharing options...
salathe Posted February 2, 2011 Share Posted February 2, 2011 You're not seeing the true output from print_r() there. Since XML tags are being printed, your browser is thinking they're HTML tags and not displaying them. Either output your page as plain text (by sending the appropriate Content-Type header, or specifying the default_mimetype PHP.ini setting in your script) or "view source" in your browser. Quote Link to comment https://forums.phpfreaks.com/topic/226366-loadxmldomxpath-problem/#findComment-1168780 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.