tags:

views:

45

answers:

3

Hi there here is my xml file: (thefile)

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://nts-de-osm1-pxc/webservices/"&gt;
  <Item xmlns:q1="http://systinet.com/wsdl/com/osm/webservices/service/" xsi:type="q1:Document">

    <q1:attributes>
      <q1:Attribute>
        <q1:dataDictionary xsi:nil="true" />
        <q1:dataType>string</q1:dataType>
        <q1:displayName>AEND_DATUM</q1:displayName>
        <q1:key>false</q1:key>
        <q1:name>AEND_DATUM</q1:name>
        <q1:searchable>true</q1:searchable>
        <q1:userAttribute>true</q1:userAttribute>
        <q1:value>10.05.10</q1:value>
      </q1:Attribute>
      <q1:Attribute>
        <q1:dataDictionary xsi:nil="true" />
        <q1:dataType>string</q1:dataType>
        <q1:displayName>AEND_PRUEF_DATUM</q1:displayName>
        <q1:key>false</q1:key>
        <q1:name>AEND_PRUEF_DATUM</q1:name>
        <q1:searchable>true</q1:searchable>
        <q1:userAttribute>true</q1:userAttribute>
        <q1:value>10.05.10</q1:value>
      </q1:Attribute>
    </q1:attributes>
  </Item>
</ArrayOfItem>

Here is my code

$xml = simplexml_load_file($thefile);
print_r($xml);

This is the output

SimpleXMLElement Object
(
    [Item] => SimpleXMLElement Object
        (
        )

)

Why is this Empty?

+1  A: 

The simpleXML parser should provide a meaningful error message that you can fetch and output. Check out the example code in the documentation on libxml_get_errors().

Pekka
There is no error message. I think the file is valid xml.
sschnake
@sschnake @pinaki is pointing out at least one grave error, isn't he? (the missing closing tag)
Pekka
Yeah, the XML was invalid (I have fixed the original post) and raised at least 3 PHP warnings. Check your error_reporting level. Try adding `error_reporting(-1);` at the beginning of your scripts, otherwise you'll miss so many of those errors.
Josh Davis
A: 

Two things:

  • You are missing the closing tag for q1:attributes.
  • SimpleXML cannot handle <q1:Attributes> type tags. I changed it to <q1> and it works till that tag.

I think you need SimpleXML parser here

pinaki
I have fixed the XML. As for the rest, SimpleXML supports namespaces, no problem here.
Josh Davis
+3  A: 

Don't use print_r() or var_dump() to inspect a SimpleXMLElement, they won't necessarily work on them because SimpleXML uses lots of magic behind the scene. Instead, look at what asXML() returns.

In your case, it doesn't show <q1:attributes/> because they're not in the same namespace.

Edit

To access those namespaced nodes, there are many different ways, most of them discussed here at Stack Overflow. If you can't work it out, please open a new question, since the subject is different. Here's 3 ways to access those elements:

$ArrayOfItem->Item->children("http://systinet.com/wsdl/com/osm/webservices/service/");
$ArrayOfItem->Item->children('q1', true);
$ArrayOfItem->Item->xpath('//q1:Attribute');
Josh Davis
sXML() shows me the xml file, but how to select a single node now? echo $xml->ArrayOfItem->Item->attributes->Attribute[0]->value; dosen't show anything
sschnake
Updated.```````````````
Josh Davis