Problem in XML content..?
Okay. Here's the code.
XML Parsing Error: not wel开发者_高级运维l-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.
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 &
automatically. SO you might not need the CDATA sections at all.
精彩评论