XML Type Format Inclusion of Font Attributes

When I initially posed this question here: question here, I was given this suggested code that I thought worked fine.
<?php // /demo/temp_pkonstan2.php

/**
 * Some XML-related ideas
 * See: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28668382.html
 */
error_reporting(E_ALL);

// A MALFORMED XML DOCUMENT FROM THE QUESTION AT EE
$xml = <<<EOD
<ttl>This is the Title</ttl>
<sub>This is a sub-title</sub>
<bod>This is a body with <em>Italics in the middle</em></bod>
<con>This creates a continue button</con>
EOD;

// CORRECT THE FORMATION OF THE XML
$xml = '<wrap>' . $xml . '</wrap>';

// TRY TO MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// SHOW THE PROPERTIES OF THE OBJECT
echo '<pre>';
foreach ($obj as $key => $value)
{
    echo PHP_EOL . 'obj->' . "$key = " . $value;
}

Open in new window


But upon closer inspection, the code in the third line that begins with <bod> cuts off when it encounters the <em>.  The output looks like this:

obj->ttl = This is the Title
obj->sub = This is a sub-title
obj->bod = This is a body with
obj->con = This creates a continue button

I need some help in how to fix this. ]
The third line should look like this:
obj->bod = This is a body with <em>Italics in the middle</em>

Thanks.
Paul KonstanskiProject SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Sorry about that - I did not notice the embedded tags in the original question.  You cannot put the wicket characters into XML documents.  They need to be encoded so that they do not collide with the XML tags.  This usually does the trick - pass the data through before putting it into the XML document.
http://php.net/manual/en/function.htmlspecialchars.php
Ray PaseurCommented:
An alternative, of course, is to use JSON instead of XML.  JSON is much "lighter" as a transport mechanism and is the preferred encoding today.
Ray PaseurCommented:
This seems right.  Please post back if you find any issues or have questions.
<?php // /demo/temp_pkonstan3.php

/**
 * Some XML with embedded tags
 * See: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28678487.html
 * http://php.net/manual/en/function.htmlspecialchars.php
 */
error_reporting(E_ALL);

// THE RAW MATERIALS FOR MAKING AN XML DOCUMENT
$ttl = 'This is the Title';
$sub = 'This is a sub-title';
$bod = 'This is a body with <em>Italics in the middle</em>';
$con = 'This creates a continue button';

// ESCAPING THE PART THAT CONTAINS SPECIAL CHARACTERS
$escaped_bod = htmlspecialchars($bod);

// CREATE AN XML FRAGMENT
$xml = <<<EOD
<ttl>$ttl</ttl>
<sub>$sub</sub>
<bod>$escaped_bod</bod>
<con>$con</con>
EOD;

// WRAP THE FRAGMENT XML
$xml = '<wrap>' . $xml . '</wrap>';

// TRY TO MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// SHOW THE PROPERTIES OF THE OBJECT
echo '<pre>';
foreach ($obj as $key => $value)
{
    echo PHP_EOL . 'obj->' . "$key = " . $value;
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.