...

View Full Version : formatting the data



tannu
06-26-2007, 02:14 PM
Hello all,

I am having a PHP script that generates the xml using the data in a mysql database. We have "nl2br" function in PHP that preserves the format (<br /> <p> etc..). When i use this method to generate the xml, the xml file that gets generated contains the <br /> tag in the node tree. Is there any other way to preserve the format even in the generated xml, without having the <br /> tags.

Following is the PHP:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("xplane");
$query = "SELECT * FROM categorythreads";
$result = mysql_query($query);
header("Content-type: text/xml");
$xml = "<?xml version='1.0' encoding='utf-8'?>";
$xml .= "<category>";
while($fetchedThreads = mysql_fetch_object($result))
{
$xml .= "<threads>";
$xml .= "<threadID>" .$fetchedThreads -> threadID. "</threadID>";
$xml .= "<threadCategory>" .$fetchedThreads -> threadCategory ."</threadCategory>";
$xml .= "<threadTitle>" .$fetchedThreads -> threadTitle ."</threadTitle>";
$xml .= "<threadDetails>" .$fetchedThreads -> threadDetails ."</threadDetails>";
$xml .= "<postedDate>" .$fetchedThreads -> postedDate ."</postedDate>";
$xml .= "<permaLink>" .$fetchedThreads -> permaLink ."</permaLink>";
$xml .= "</threads>";
}
$xml .= "</category>";
echo nl2br($xml);
mysql_free_result($result);
mysql_close();
?>

generated xml:
<category>
<threads>
<threadDetails>"I typically use this column to look at future trends."
<br />
"I typically use this column to look at future trends."</threadDetails>
</threads>
</category>

Javascript statement that places the nodevalue:
var threadDetails = result.getElementsByTagName("threadDetails")[i].firstChild.nodeValue;
document.getElementById("thread").innerHTML = threadDetails;

html:
<div id="thread">"I typically use this column to look at future trends."</div>

expected html:
<div id="thread">
"I typically use this column to look at future trends."
"I typically use this column to look at future trends."
</div>

please do help me out,

thanks in advance,
tannu

A1ien51
06-26-2007, 02:16 PM
Try to use \n instead of <br>

Eric

tannu
06-26-2007, 02:43 PM
<br /> tag is added by the php script generating the xml. Basically what i am trying to do is to fetch and display the two paragraphs stored in the database. Please tell me how to make the xml parser to display two seperate lines as two seperate lines instead of one.
ex:
<?xml version="1.0" encoding="utf-8" ?>
<category>
<thread>
"This is first line..."
"This is second line..."
</thread>
</category>
if i use javascript to read this xml and display the output, the output shuld read
"This is first line..."
"This is second line..."

not
"This is first line...""This is second line..."

please do help me out,

thanks,
tannu

A1ien51
06-26-2007, 03:14 PM
document.getElementById("thread").innerHTML = threadDetails.replace(/\n/gi,"<br/>");

Eric

tannu
06-26-2007, 04:03 PM
Hello Eric,

its not working.

dynamically generated xml is (thru php, using data stored in mysql, two parargraphs that read:"I typically use this column to look at future trends.")

<category>
<threads>
<threadDetails>"I typically use this column to look at future trends."
<br />
"I typically use this column to look at future trends."</threadDetails>
</threads>
</category>

I am using DOM to generate the div and related elements and appending the related result. Following is the snippet:

xmlhttp_categoryThreads is XMLHTTPRequest object

result = result = xmlhttp_categoryThreads.responseXML;

var threadDetails = result.getElementsByTagName("threadDetails")[i].firstChild.nodeValue;
var p_threadDetails = document.createElement("p");
var txt_threadDetails = document.createTextNode(threadDetails);
p_threadDetails.appendChild(txt_threadDetails);
div_categoryThread.appendChild(p_threadDetails);

intended o/p is:
<div id="div_categoryThread">
"I typically use this column to look at future trends."
"I typically use this column to look at future trends."
</div>

o/p i am getting:
<div id="div_categoryThread">
"I typically use this column to look at future trends."
</div>

the problem, what i think may be the <br /> tag that the XML is having,

is there any way to generate the xml having the same format as the data fetched by the database?


the data stored in the mysql:
"I typically use this column to look at future trends."
"I typically use this column to look at future trends."

I am using the nl2br formatting function in php, which basically preservs the format, in this case a <br /> tag.

please tell me where i am going wrong, and suggest any other alternative to achieve the desired result.

thanks again,
tannu

A1ien51
06-26-2007, 04:23 PM
Man I am not with it today! LOL

If you have HTML tags in there, you should look into using cdata

Eric

rfresh
06-27-2007, 02:12 AM
Remember for small data sets with ajax you do not have to use XML at all. I programmed an ajax site using PHP and just used comma delimited data strings so I could parse them easily back in the client with JS. XML wasn't needed at all to pass the data back to the client thru the "ajax tunnel".



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum