Hi I cant quite get the coding right to extract this one string (hostPIN) from the XML response below. I've been searching and searching and cant get anything to work. Can anyone direct me on how to extract that data using PHP? Thanks!
I am posting just the path to the data I need to get because the xml data is quite large.
1234
How do I extract 1234? The namespaces are messing me up and I've read post after post but can't find one that would work with this.
Thanks for any help anyone can provide!
Answer
This is a base example with DOMDocument
:
$dom = new DOMDocument();
$dom->loadXML( $xmlString, LIBXML_NOBLANKS );
$nsURI = $dom->lookupNameSpaceUri( 'use' );
foreach( $dom->getElementsByTagNameNS ( $nsURI , 'hostPIN' ) as $node )
{
echo $node->nodeValue.PHP_EOL;
}
If you will load xml from a file/url instead that from a string, use $dom->load
instead of $dom->loadXML
.
$node->nodeValue
is the value that you want.
Please note:
In the example above, $dom->lookupNameSpaceUri( 'use' )
may not work, depending on where the namespace is declared. In this case, you can find by you the namespaceURI (it's declared somewhere in document as xmlns:use="http://example.com/someurl"
) and replace this line:
$nsURI = $dom->lookupNameSpaceUri( 'use' );
whit this:
$nsURI = "http://example.com/someurl";
No comments:
Post a Comment