dtra49684 2009-07-21 20:27
浏览 25
已采纳

从PHP中的XML .NET DataSet获取数据

I have the following XML from a .NET web service:

<?xml version="1.0" encoding="utf-8"?>
<DataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="GROUP_ID" type="xs:int" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <NewDataSet xmlns="">
      <Table1 diffgr:id="Table11" msdata:rowOrder="0" diffgr:hasChanges="inserted">
        <GROUP_ID>NUM</GROUP_ID>
      </Table1>
    </NewDataSet>
  </diffgr:diffgram>
</DataSet>

I need to access just the GROUP_ID node value, but want to know the easiest way to do this. So far I have been using DomDocument and DomXPath to query for that node, but I'm hoping there is a better way that I am missing:

$xml = new DomDocument();
$xml->load('file.xml');
$xp = new DomXPath($xml);
$result = $xp->query('//GROUP_ID');
$id = null;
foreach($result as $node){
  $id = $node->nodeValue;
  break;
}

Thanks!

  • 写回答

1条回答 默认 最新

  • dssqq64884 2009-07-21 20:48
    关注

    The need to loop in some fashion is unfortunately necessary. If you were using simplexml you could write your code as simply as:

    $xml = simplexml_load_file('file.xml');
    $result = $xml->xpath('//GROUP_ID');
    while(list(,$node) = each($result)) {
        $id = (string) $node;
        break; // optional
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题