• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 109
  • Last Modified:

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
0
Paul Konstanski
Asked:
Paul Konstanski
  • 2
  • 2
1 Solution
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now