2014-04-07 144 views
0

下面你可以看到我想要解析的XML文件,但它似乎并没有深入到 。用Dom解析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> 

回答

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