Novy
Novy

Reputation: 334

SimpleXMLElement using a string

I want to create a new SimpleXMLElement with data . When I put the data from the link below in the code I get the next error: Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML

The encoded data can be found here:http://www.interwebmedia.nl/dataxi/base64.txt

decoded data: http://www.interwebmedia.nl/dataxi/data.txt

<?php
str = 'encodeddata';
//echo htmlspecialchars(base64_decode($str),ENT_QUOTES);
$decoded = htmlspecialchars(base64_decode($str),ENT_QUOTES);
$xml = new SimpleXMLElement($decode);
echo $xml->asXML();
?> 

Upvotes: 0

Views: 63

Answers (1)

IMSoP
IMSoP

Reputation: 97688

I think you've attempted to use HEREDOC syntax (or seen somebody else using it) but completely misunderstood it.

HEREDOC syntax is an alternative way of quoting a string, instead of " or '. It's useful for hard-coding blocks of XML, because it acts like double-quotes, but let's you use double-quotes inside, like this:

$my_xml_string = <<<XML
<some_xml>
<with multiple_lines="here" />
</some_xml>
XML;

That code is precisely equivalent to this:

$my_xml_string = "
<some_xml>
<with multiple_lines=\"here\" />
</some_xml>
";

What you have done instead is taken the literal string "<<<" and added it onto your XML, giving you a string like this:

$my_xml_string = "<<<XML
<some_xml>
<with multiple_lines=\"here\" />
</some_xml>
XML";

Or in your example, the string "<<<XML<data>XML".

As far as the XML parser's concerned, you've just put a load of garbage on the beginning and end of the string, so it rightly complains it's not a valid XML document.

Upvotes: 1

Related Questions