【问题标题】:Read node within namepace Simplexml [closed]使用命名空间 Simplexml 读取节点 [关闭]
【发布时间】:2012-07-05 03:44:27
【问题描述】:

我有以下 xml 代码:

文件 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>

我如何读取数据之间的数据

我有以下 php 代码

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


?> 

但这不起作用。

谁能帮我访问一个节点,它是命名空间元素的子节点。

亲切的问候

【问题讨论】:

    标签: php namespaces simplexml xml-namespaces


    【解决方案1】:

    哦,从哪里开始。此工作代码可能说明了一些问题:

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

    举几个问题:

    1. xmlns="" 与没有命名空间不同,而且,在我看来,只是......好吧,很奇怪。
    2. 您的 foreach 为时过早(应在根元素 (Dataset) 上调用 children('diffgr')
    3. 如果您只对命名空间前缀感兴趣,只需将children() 的第二个参数设置为true

    【讨论】:

    • 呵呵,从某种意义上说,我很高兴那里少了一个xmlns="",然后我想;)foreach($weirdemptynamespace-&gt;dsAanbod-&gt;Pand as $kot){echo $kot-&gt;PandID;}应该可以解决问题吗?
    猜你喜欢
    • 2011-09-28
    • 2012-11-10
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 2021-10-09
    • 2011-03-27
    • 2012-01-06
    相关资源
    最近更新 更多