XML Type Format to

I have an application that is made up of XML type entries in a database.  All XML codes are three letters long. So an entry looks like this.

<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>

Open in new window


So when you do the database query for this record you get back a string that is made up  series of lines that all begin with an XML type tag and end with the tag closed.  These tags are then interpreted by a function to mean different things. It's worked fine for years in English, but now that I'm expanding to French and Spanish I'm running into problems.

Where I need to get to is key paired array that would consist of the key being the three letter code and the value being the text between the code.  

I use this command to create the initial array where each line is broken into a separate record in the array:
	$str = explode("\n", $databaseTxt);

Open in new window


So what I need is the above code to create an object that looks like this:

$obj->ttl = This is the Title
$obj->sub = This is a sub-title
$obj->bod = This is a body with <em>Italics in the middle</em>
$obj->con = This creates a continue button

Open in new window


The previous way I was doing it was like this, but this is now crashing with the utf8 encoding in the database for the french and spanish.

	$str = explode("\n", $databaseTxt);
	foreach ($str as $txt) {
		if ($txt > "") $xmlReady .= substr($txt, 0,5).'<![CDATA['.substr($txt, 5, -7).']]>'.substr($txt, -7, 7);
	} // end foreach ($str as $txt) {

	$xmlstring = '<?xml version="1.0"?>
	<scr_'.$screen.'>
	'.$xmlReady.'
	</scr_'.$screen.'>';

	#$xml = simplexml_load_string$xmlstring); // this was the old way that worked fine before I converted table to UTF8
	$xml = simplexml_load_string(utf8_encode($xmlstring));

Open in new window


But I know there has to be a better way...  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:
See if this helps...
http://iconoun.com/demo/temp_pkonstan2.php

<?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

0

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
Paul KonstanskiProject SpecialistAuthor Commented:
That was excellent. Much easier.

Here's all it took to make it work for me after your suggestion. Because I'm using foreign language characters, I had to add the utf8_endocde to the XML.


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

      // TRY TO MAKE AN OBJECT
      $obj = SimpleXML_Load_String(utf8_encode($xml));
      foreach($obj as $curName => $line) {
0
Ray PaseurCommented:
Yay, glad it's working better now.  Getting the XML document right is one part of it; getting the encoding right is a separate part.
0
Paul KonstanskiProject SpecialistAuthor Commented:
Upon closer inspection, I just realized that this script is not working correctly. I've posted the problem in a new question here:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/Q_28678487.html
0
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
PHP

From novice to tech pro — start learning today.