3

I am new to simplexml parser in PHP. I have run the examples that I have found and they work as advertised. I can't get it to work with my program though. I have searched in here for hours.

I have an XML file called core.xml (note that the node tags have colons in):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <dc:title>Lecture 3</dc:title>
    <dc:creator>John Brown &amp; Greg Smith</dc:creator>
    <cp:lastModifiedBy>Greg Smith</cp:lastModifiedBy>
    <cp:revision>165</cp:revision>
    <dcterms:created xsi:type="dcterms:W3CDTF">2010-02-19T04:37:55Z</dcterms:created>
    <dcterms:modified xsi:type="dcterms:W3CDTF">2014-01-30T02:41:30Z</dcterms:modified>

</cp:coreProperties>

I use following code to load it into the parser:

if (file_exists('core.xml')){

    echo 'file exists <br>';
    $xml_core = simplexml_load_file('core.xml');

    print_r($xml_core);
} 
else 
{    
   exit('Failed to open core.xml.');  
}

The file exists but all I get at the print_r is:

file exists    
SimpleXMLElement Object ( ) 

How do I access the nodes? Other XML files that I have to use are many layers in depth.

Thanks

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
KangaStu
  • 31
  • 2

2 Answers2

10

What you describe as an error pattern in your question is actually correct behaviour and not an error.

If you use print_r (or var_dump for that matter) on a SimpleXMLElement it will only show you some rudimentary information about that object and not the entire content of the XML document.

To see the whole XML, use the asXML() method instead. Alternatively to obtain debug information you can use a library that is specifically aware on how to debug SimpleXMLElement content's like simplexml_debug written by IMSop.

In your specific case, the object shows empty (no object members) because there aren't any nodes in the XML document within the default namespace (see: Namespaces in XML 1.0) that - after the more or less complex rules of SimpleXMLElement to array transitions were applied - would have been passed from the objects internal debug handler to the print_r or var_dump function to be displayed then. So only the class-name is given so that you can see it's an object of a certain class - and has no members:

SimpleXMLElement Object ( ) 
        ^          ^     ^ 
   object type     |     `------ object fields (empty)
                   |
             variable type

How to deal with that? It's very simple: Know the XML (you know it aleady) and obtain the data from it as it's documented:

As your XML element names contain colons, you should be made aware that these are so called XML namespaces. As you've seen differences in the print_r output, you will also see differences in accessing the child elements within that namespace. You have to use the children() method and specify the namespace of which you would like to obtain the child elements. If you use the xpath() method to access elements, you need to register an namespace prefix before you use it on the SimpleXMLElement you call that method on.

Some assorted selection of SimpleXML and XML Namespace related Q&A material here on site:

You can also obtain this information from the PHP manual, just take a look for namespace related information within the SimpleXMLElement API documentation.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
0

You can use file_get_contents to load XML file.

Try

if(file_exists('core.xml'))
{
    echo 'file exists <br>';
    $xml_core = file_get_contents('core.xml');
    $xml = new SimpleXMLElement($xml_core);
    print_r($xml);
}
else 
{
   exit('Failed to open core.xml.');
}
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50