【问题标题】:Parsing XML with Dom用 Dom 解析 XML
【发布时间】:2014-05-19 20:07:36
【问题描述】:

您可以在下面看到我正在尝试解析的 XML 文件,但它似乎没有解析 够深。

我想要的是获取Kunde 节点并获取其子节点的值。这是什么 mycode 到目前为止的样子:

foreach($xml->childNodes AS $test){
  $m = new Karte();
  $m->setPDateCreate($test->childNodes->item(0)->nodeValue);
  $m->setPDateModify($test->childNodes->item(1)->nodeValue);
  $m->setPDateAcess($test->childNodes->item(2)->nodeValue);
}

现在的问题是第一项包含所有Kunde 节点及其内部的值。

这是 XML 文件:

<?xml version="1.0"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <SOAP-ENV:Body>
        <JCExport xmlns="https://whatever/">
            <Kunden>
                <Kunde>
                    <KDateCreate>26.06.2013 17:25:55</KDateCreate>
                    <KDateModify>26.06.2013 17:25:55</KDateModify>
                    <KDateAccess>26.06.2013 17:25:55</KDateAccess>
                </Kunde>
                <Kunde>
                    <KDateCreate>26.06.2013 17:25:55</KDateCreate>
                    <KDateModify>26.06.2013 17:25:55</KDateModify>
                    <KDateAccess>26.06.2013 17:25:55</KDateAccess>
                </Kunde>
                <Kunde>
                    <KDateCreate>26.06.2013 17:25:55</KDateCreate>
                    <KDateModify>26.06.2013 17:25:55</KDateModify>
                    <KDateAccess>26.06.2013 17:25:55</KDateAccess>
                </Kunde>
                <Kunde>
                    <KDateCreate>26.06.2013 17:25:55</KDateCreate>
                    <KDateModify>26.06.2013 17:25:55</KDateModify>
                    <KDateAccess>26.06.2013 17:25:55</KDateAccess>
                </Kunde>
                <Kunde>
                    <KDateCreate>26.06.2013 17:25:55</KDateCreate>
                    <KDateModify>26.06.2013 17:25:55</KDateModify>
                    <KDateAccess>26.06.2013 17:25:55</KDateAccess>
                </Kunde>
            </Kunden>
        </JCExport>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

【问题讨论】:

    标签: php xml dom


    【解决方案1】:

    当您处理 xml 命名空间时,我建议使用 DOMXPath,例如:

    <?php
    $xml = new DOMDocument();
    $xml->load('soap.xml');
    
    $xpath = new DOMXPath($xml);
    $xpath->registerNamespace('SOAP-ENV', 'http://schemas.xmlsoap.org/soap/envelope/');
    $xpath->registerNamespace('whatever', 'https://whatever/');
    
    foreach ($xpath->query('/SOAP-ENV:Envelope/SOAP-ENV:Body/whatever:JCExport/whatever:Kunden/whatever:Kunde') as $customer) {
      $m = new Karte();
    
      foreach ($customer->childNodes as $node) {
        switch ($node->nodeName) {
          case 'KDateCreate':
            $m->setPDateCreate($node->nodeValue);
            break;
    
          case 'KDateModify':
            $m->setPDateModify($node->nodeValue);
            break;
    
          case 'KDateAccess':
            $m->setPDateAccess($node->nodeValue);
            break;
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      相关资源
      最近更新 更多