Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    formatting the data

    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

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Try to use \n instead of <br>

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <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
    Last edited by tannu; 06-26-2007 at 02:51 PM. Reason: was not complete

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    document.getElementById("thread").innerHTML = threadDetails.replace(/\n/gi,"<br/>");

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    New Coder
    Join Date
    Feb 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Man I am not with it today! LOL

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

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #7
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    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".
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •