dongti8535
2016-02-05 01:03
浏览 50

使用命名空间从XML中提取数据

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.

<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 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!

图片转代码服务由CSDN问答提供 功能建议

您好我无法正确编码从下面的XML响应中提取这一个字符串(hostPIN)。 我一直在寻找和搜索,无法得到任何工作。 任何人都可以指导我如何使用PHP提取数据? 谢谢!

我只发布了我需要获取的数据的路径,因为xml数据非常大。

 &lt;  serv:message&gt; 
&lt; serv:body&gt; 
&lt; serv:bodyContent xsi:type =“use:getUserResponse”&gt; 
&lt; use:personalMeetingRoom&gt; 
&lt; use:hostPIN&gt; 1234&lt; / use  :hostPIN&gt; 
   
 
 

如何提取1234? 命名空间让我感到困惑,我已经发布了帖子后发帖,但找不到可以使用的帖子。

感谢任何人都可以提供帮助! \ n

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doujie7346 2016-02-05 01:38
    已采纳

    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";
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongyishen5796 2016-02-05 03:27

    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;
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题