1

I am making get request in PHP but the parser is having issues. I am looking for a way to retreive the value of URL in the following example. The response xml looks like this.

<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<testtemplate JobName="test1">
<Sources>
 <Source xmlns="http://www.microsoft.com/test/schema/api">
    <System>
      <SystemIdentifier>HTTPS</SystemIdentifier>
      <URL>http://example.com</URL>
    </System>
 </Source>
</Sources>
<testtemplate>

and my PHP code looks like this:

$xml = DOMDocument::LoadXML($response);
$mypath = new DOMXPath($xml);
$mypath->registerNamespace("a", "http://www.microsoft.com/test/schema/api");

$url = $mypath->evaluate("/testtemplate/Sources/Source/System/URL");
$message = $url->item(0)->value;

print($message);

Any help would be appreciated.

Thanks Ashish

1
  • 1
    What are the "issues" it is having? Commented Jun 5, 2010 at 21:13

1 Answer 1

2

Your sample XML is malformed (the testtemplate is not closed), assuming that is just a typo lets get on to fixing your main issue.

You'll need to use that namespace which was registered in order to access the elements within it, and the DOMElement from item(0) does not have a value property (nodeValue is used instead).

$xml = DOMDocument::LoadXML($response);
$mypath = new DOMXPath($xml);
$mypath->registerNamespace("a", "http://www.microsoft.com/test/schema/api");

// These two lines have changed, slightly.
$url = $mypath->evaluate("/testtemplate/Sources/a:Source/a:System/a:URL");
$message = $url->item(0)->nodeValue;

print($message);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.