1

I want to read this xml document:

<?xml version="1.0" encoding="UTF-8"?>
<tns:getPDMNumber xmlns:tns="http://www.testgroup.com/TestPDM" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.testgroup.com/TestPDM getPDMNumber.xsd ">
  <tns:getPDMNumberResponse>
    <tns:requestID>22222</tns:requestID>
    <tns:pdmNumber>654321</tns:pdmNumber>
    <tns:responseCode>0</tns:responseCode>
  </tns:getPDMNumberResponse>
</tns:getPDMNumber>

I tried it this way:

$dom->load('response/17_getPDMNumberResponse.xml'); 

$nodes = $dom->getElementsByTagName("tns:requestID");
//$nodes = $dom->getElementsByTagName("tns:getPDMNumber");
//$nodes = $dom->getElementsByTagName("tns:getPDMNumberResponse");
foreach($nodes as $node)
{       
    $response=$node->getElementsByTagName("tns:getPDMNumber");
    foreach($response as $info)
    {
        $test = $info->getElementsByTagName("tns:pdmNumber");
        $pdm = $test->nodeValue;
    }
}

the code never runs into the foreach loop.

Only for clarification my goal is to read the "tns:pdmNumber" node.

Have anybody a idea?

EDIT: I have also tried the commited lines.

0

2 Answers 2

2

The XML uses a namespace, so you should use the namespace aware methods. They have the suffix _NS.

$tns = 'http://www.testgroup.com/TestPDM';
$document = new DOMDocument();
$document->loadXml($xml);
foreach ($document->getElementsByTagNameNS($tns, "pdmNumber") as $node) {
  var_dump($node->textContent);
}

Output:

string(6) "654321"

A better option is to use Xpath expression. They allow a more comfortable access to DOM nodes. In this case you have to register a prefix for the namespace that you can use in the Xpath expression:

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('t', 'http://www.testgroup.com/TestPDM');

var_dump(
  $xpath->evaluate('string(/t:getPDMNumber/t:getPDMNumberResponse/t:pdmNumber)')
); 
Sign up to request clarification or add additional context in comments.

Comments

1

This:

$nodes = $dom->getElementsByTagName("tns:requestID");    

you find all the requestID nodes, and try to loop on them. That's fine, but then you use that node as a basis to find any getPDMNumber nodes UNDER the requestID - but there's nothing - requestID is a terminal node. So

$response=$node->getElementsByTagName("tns:getPDMNumber");

finds nothing, and the inner loop has nothing to do.

It's like saying "Start digging a hole until you reach china. Once you reach China, keep digging until you reach Australia". But you can't keep digging - you've reached the "bottom", and the only thing deeper than China would be going into orbit.

1 Comment

thank you for your answer. Please look at my edit section above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.