'php DOMDocument get part of xml as xml

i have the following xml

<HEAD>
    <Body attribute3="3" attribute1="1" attribute2="2">
        <Response id="4" status="OK">
            <Token c="c" b="b" a="a" e="e" d="d">
            </Token>
        </Response>
    </Body>
 </HEAD>

and i used simplexml to manipulate it.The problem is that i have to Canonicalize XML nodes which is supported by DOMDocument.

With simplexml i used $xml->Body->Response->asXML(); to get the Response node as XML.

I am trying to get Response node as XML with DOMDocument but i don't know how

so far i have

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($Response);
$xmlDoc->C14N();
$xmlDoc->formatOutput = true;
$xml_string = $xmlDoc->saveXML();

an in $xml_string i have the xml

i want to get Response node as XML with DOMDocument

<Response id="4" status="OK">
    <Token c="c" b="b" a="a" e="e" d="d">
    </Token>
</Response>

can anyone help me how to do that

Any help appreciated



Solution 1:[1]

If I understand you correctly, this should get you there:

$XMLDoc = new DOMDocument();
$XMLDoc->loadXML($response);
$xpath = new DOMXPath($XMLDoc);

$target = $xpath->query('//Response');
echo $XMLDoc->saveXML($target[0]);

Output:

<Response id="4" status="OK">
            <Token c="c" b="b" a="a" e="e" d="d">
            </Token>
        </Response>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jack Fleeting