简体   繁体   English

使用名称空间从XML提取数据

[英]Extract data from XML with namespaces

Hi I cant quite get the coding right to extract this one string (hostPIN) from the XML response below. 嗨,我还不太了解从下面的XML响应中提取一个字符串(hostPIN)的编码权。 I've been searching and searching and cant get anything to work. 我一直在搜寻,却找不到任何工作。 Can anyone direct me on how to extract that data using PHP? 谁能指导我如何使用PHP提取数据? Thanks! 谢谢!

I am posting just the path to the data I need to get because the xml data is quite large. 我只发布了我需要获取的数据的路径,因为xml数据很大。

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

How do I extract 1234? 如何提取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! 感谢您提供任何帮助!

Thanks @fusion3k after a few more tweaks I got it to work with 感谢@ fusion3k再进行几次调整后,我可以使用它

$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;
}

This is a base example with DOMDocument : 这是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 . 如果要从文件/ URL而不是从字符串加载xml,请使用$dom->load而不是$dom->loadXML

$node->nodeValue is the value that you want. $node->nodeValue是所需的值。

Please note: 请注意:

In the example above, $dom->lookupNameSpaceUri( 'use' ) may not work, depending on where the namespace is declared. 在上面的示例中,取决于声明名称空间的位置, $dom->lookupNameSpaceUri( 'use' )可能不起作用。 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: 在这种情况下,您可以找到自己的namespaceURI(在文档中以xmlns:use="http://example.com/someurl" )并替换以下行:

$nsURI = $dom->lookupNameSpaceUri( 'use' );

whit this: 这:

$nsURI = "http://example.com/someurl";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM