1

Okay. Here's the code.

XML Parsing Error: not well-formed
<item_to_page_title><![CDATA[Breaking news, real-time scores and daily analysis from Sports Illustrated  SI.com]]></item_to_page_title> 

The error tracking line thing points to the []-like char as the problem. I've set the page to UTF-8 and still it doesn't work.

No, I can't just take the []-like car out. This is an API I'm working on, so I can't edit the content. Only deliver it. Is there a workaround? The CDATA tags aren't doing a thing here..

-EDIT-

This is the only line in the output error. Here's the PHP source:

        $output .= "<activity>\n";
        $result = mysql_query($query, $con);
        while($row = mysql_fetch_array($result)){
            $result2 = mysql_query("SELECT * FROM sites WHERE id = '".$row['u_to']."'", $con);  
            $row2 = mysql_fetch_array( $result2 );

            $output .= "<item> \n";
            $output .= "<item_type>" . $row['u_type'] . "</item_type> \n";
            $output .= "<item_to_page_id>" . $row['u_to'] . "</item_to_page_id> \n";
            $output .= "<item_to_page_url><![CDATA[".$row2['s_url']."]]></item_to_page_url> \n";
            $output .= "<item_to_page_title><![CDATA[".$row2['s_title']."]]></item_to_page_title> \n";
            $output .= "<item_date>" . $row['u_date'] . "</item_date> \n";
            $output .= "</item> \n";
        }
        $output .= "</activity>\n";

The MySQL queries and such work fine. If I limit the MySQL query (not included in that snippet) so that it doesn't output as many results (thus meaning that the result with the error causing character isn't returned), it works fine. But I need this to be bulletproof, so I need to find a workaround for this character ... or whatever is causing the problem.

7
  • Is there nothing else arround this line ? Maybe the error is reported on the wrong line, and is a couple of lines before of after this one ? Commented Jul 16, 2010 at 20:57
  • Definitely nothing wrong with that snippet of XML you posted. Is that the whole thing or just a piece? Might help to post the whole chunk of XML if you've got it. Commented Jul 16, 2010 at 21:01
  • i editted to give you the source, but that line is the only line that the error is giving me. Commented Jul 16, 2010 at 21:06
  • 1
    How about using XMLWriter or DOM for creating the XML? Commented Jul 16, 2010 at 21:08
  • Question is: is this error from you own code, or of some remote API incabable of parsing CDATA? There are a lot out there of the second variety. Commented Jul 16, 2010 at 21:22

1 Answer 1

1

Try it with DOM, just to see if it makes a difference:

$dom = new DOMDocument;
$dom->formatOutput = TRUE;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML('<activity/>');

$result = mysql_query($query, $con);
while($row = mysql_fetch_array($result)){

    $result2 = mysql_query("SELECT * FROM sites WHERE id = '".$row['u_to']."'", $con);  
    $row2 = mysql_fetch_array( $result2 );

    $item = $dom->createElement('item');

    $item->appendChild(
        $dom->createElement('item_type', $row['u_type']));

    $item->appendChild(
        $dom->createElement('item_to_page_id', $row['u_to']));

    $cData = $dom->createCDATASection($row2['s_url']);
    $node  = $dom->createElement('item_to_page_url');
    $node->appendChild($cData);
    $item->appendChild($node);

    $cData = $dom->createCDATASection($row2['s_title']);
    $node  = $dom->createElement('item_to_page_title');
    $node->appendChild($cData);
    $item->appendChild($node);

    $item->appendChild(
        $dom->createElement('item_date', $row['u_date']));

    $dom->documentElement->appendChild($item);
}
echo $dom->saveXML();

On a sidenote: DOM will handle most characters you use as textnodes by itself. It will also convert entities like & to &amp; automatically. SO you might not need the CDATA sections at all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.