Jump to content

loadXML/DOMXPath problem


AndySX

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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().

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.