0

I can't quite get the coding right to extract this one string (hostPIN) from the XML response below. I've been searching and can't get anything to work. How can I extract that data using PHP?

I am posting just the path to the data I need to get because the XML data is quite large.

<serv:message>
 <serv:body>
  <serv:bodyContent xsi:type="use:getUserResponse">
   <use:personalMeetingRoom>
     <use:hostPIN>1234</use:hostPIN>

How do I extract 1234? The namespaces are confusing and I can't find some code that would work with this.

2
  • You may use this as example and read your data. Commented Feb 5, 2016 at 1:11
  • Can you post a link to your complete xml file? or show the namespaces? Commented Feb 5, 2016 at 1:13

2 Answers 2

1

Thanks @fusion3k after a few more tweaks I got it to work with

$dom = new DOMDocument();
$dom->loadXML( $data, LIBXML_NOBLANKS );
$nsURI = "http://www.webex.com/schemas/2002/06/service/user";
foreach( $dom->getElementsByTagNameNS ( $nsURI , 'hostPIN' ) as $node )
{
   echo $node->nodeValue.PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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";

1 Comment

I'm not getting an echo. Let me add more info. I am sending an XML API using curl and receiving the XML response back. I'm using $data = curl_exec($ch); at the end which is receiving the response back that I'm trying to parse. Hopefully that makes sense. Thanks again for your help

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.