Chaos
Chaos

Reputation: 405

Parsing XML with Dom

Below you can see my XML file which I'm trying to parse but it doesn't seem to go deep enough.

I want is to get the Kunde node and and get the value of its childnodes. This is what mycode looks like so far:

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

The problem right now is that the first item has all the Kunde nodes and their values inside of it.

Here's the XML file:

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

Upvotes: 0

Views: 57

Answers (1)

Yoshi
Yoshi

Reputation: 54659

As you're dealing with xml-namespaces, I'd suggest using DOMXPath, e.g.:

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

Upvotes: 1

Related Questions