0

Yesterday I had already a question to parsing an xml document in php. Now the parsing works kind of, but I only get everything in a string, not an object.

My code:

$xml = simplexml_load_file('file.xml');
debug($xml); // debug from CakePHP

Empty output, as I learned from my last post:

$xml = $xml->asXML();
debug($xml);

Now I only get the output as following:

I do get the XML code, but everything in a string with ->asXML(). How can I get everything as a XML object? I tried to use the string with simplexml_load_string($xml); but that just outputs the same as in the first screenshot.

Maybe there is a problem because the XML starts with <asx:abap></asx> (namespace)?

If you want, I can upload my XML so you can test it.

Edit 1

I realized, that the first two elements in my XML are namespaced (<asx:abap> and asx:values). As soon as I renamed them to asx only, everything worked fine.

Edit 2

Here my XML for you to test.

Downloadlink to XML

6
  • Indeed. The file won't be parsed correctly because the first element is namespaced. As soon as I replaced <asx:abap></asx:abap> with <asx></asx>, I get everything as an object. How can I fix that? The XMLs are generated by SAP and search/replace before parsing is not really a nice thing. Commented Feb 10, 2016 at 10:56
  • 2
    add your xml to the post Commented Feb 10, 2016 at 11:17
  • 1
    Can't replicate. Upload example minimal XML that demonstrates the problem. Commented Feb 10, 2016 at 11:56
  • Uploaded the XML, it's in the post. Commented Feb 10, 2016 at 12:25
  • You have the right object - you can see that, because your debug starts object(SimpleXMLElement) - you just don't have the right debug function to view it - it looks like it's based on print_r, which doesn't know how to display SimpleXMLElement objects properly. Try github.com/IMSoP/simplexml_debug Commented Feb 10, 2016 at 18:55

1 Answer 1

1

Your test.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<rss> 
  <channel> 
    <item> 
        <title><![CDATA[Tom & Jerry]]></title> 
    </item> 
  </channel> 
</rss>

To load xml form file use following fucntion

$xml = simplexml_load_file('test.xml');

 // echo does the casting for you 
echo $xml->channel->item->title; 

// but vardump (or print_r) not! 
var_dump($xml->channel->item->title); 

// so cast the SimpleXML Element to 'string' solve this issue 
var_dump((string) $xml->channel->item->title); 

for more info see on this link http://php.net/manual/en/simplexml.examples-basic.php

Sign up to request clarification or add additional context in comments.

2 Comments

I believe he doesn't need strings, he clearly said it is in string and he wants it in object.
@ArrowHead The OP wanted to access the XML as an object; this shows how. The string the OP didn't want was ->asXML(), which turns the whole object back into the string it was just parsed from. However, it is a little more complicated than this example, because it uses namespaces, and therefore needs an appropriate call to the ->children() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.