0

using php to parse this xml code:

<description><![CDATA[foobar

foobar2

foobar3]]></description>

The problem is when I parse with simplexml_load_file I forget the orginal format:

$content = simplexml_load_file("my_file.xml");
$a = $content->description;
echo $a;
foobar foobar2 foobar3

How can I preserve the original format?

Note: I can't edit the xml file, I must use simplexml_load_file

user1066183
  • 2,444
  • 4
  • 28
  • 57

1 Answers1

1

Your code is (mostly) correct. You might want to force string conversion, as CDATA is tricky to manage:

$a = "{$content->description}"; // Notice the quotes and braces
echo $a;

Note that, as @NigelRen observed, your output will not appear formatted in a browser. To have it formatted you need to tell the browser HTML parser to do it:

echo "<pre>{$a}</pre>";

or use nl2br().

LSerni
  • 55,617
  • 10
  • 65
  • 107