Link to home
Start Free TrialLog in
Avatar of Paul Konstanski
Paul KonstanskiFlag for United States of America

asked on

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.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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
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.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial