doufei7516 2012-07-04 16:04
浏览 47

使用命名空间Simplexml读取节点[关闭]

I have the following xml code:

File 1.xml

<?xml version="1.0" encoding="utf-8"?>
 <DataSet xmlns="">
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
   <dsAanbod>
    <Pand diffgr:id="Pand1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
      <PandID>231384</PandID>
    </Pand>
   </dsAanbod>
 </diffgr:diffgram>
</DataSet>

How can i read the data between

I had the following php code

<?php 


    $xml_url = "1.xml";
    $xml = simplexml_load_file($xml_url);

    foreach ($xml as $entry){

       //Use that namespace
       $namespaces = $entry->getNameSpaces(true);
       //Now we don't have the URL hard-coded
       $diffgr = $entry->children($namespaces['diffgr']); 
       echo $diffgr->diffgram->dsAanbod->Pand->PandID;
}


?> 

But that doesn't work.

Can somebody help me to get acces to a node where it is a child of an namespace element.

Kind Regards

  • 写回答

1条回答 默认 最新

  • douyan2470 2012-07-04 16:49
    关注

    Oh, where to begin. This working code might illustrate some of the problems:

    <?php
        $xml_url = "1.xml";
        $xml = simplexml_load_file($xml_url);
        foreach($xml->children('diffgr',true) as $diffgr){
                $weirdemptynamespace = $diffgr->children('',true);
                echo $weirdemptynamespace->dsAanbod->Pand->PandID;
       }
    

    To name a few problems:

    1. xmlns="" is NOT the same as no namespace, and, in my opinion, just.... well, weird.
    2. Your foreach was premature (children('diffgr') should be called on the root element (Dataset).
    3. If you are only interested in the namespace-prefix, just set the 2nd argument of children() to true.
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能