简体   繁体   中英

using <![CDATA in xml response

I have looked for an answer to this question but I have been unable to find an answer that works for my application.

I need to give a response from a sql query in xml format but the response is a url so i will need to use cdata. Here is my code.

if ($_POST['action']=="getSermon")
{
    //echo"You got it";
    $SQL = "SELECT * FROM sermons";

    $allsermon = mysql_query($SQL);
    $sermonrow = mysql_fetch_assoc($allsermon);

    header('Content-type: text/xml; charset=utf-8');
    header('Pragma: public');        
    header('Cache-control: private');
    header('Expires: -1');
    $xml = '<?xml version="1.0" encoding="utf-8"?>';
    echo $xml;
    echo "<SERMON_INFO>";   
    do {
        echo "<SERMON>";    
        echo "<ID>" . $sermonrow['id'] . "</ID>";
        echo "<NAME>" . $sermonrow['Name'] . "</NAME>"; 
        echo "<URL>".<![CDATA[$sermonrow['url']]>."</NAME>";
        //echo "<URL>". $sermonrow['url'] . "</URL>";   
        echo "</SERMON>";   
        mysql_free_result($result);
    } while ($sermonrow = mysql_fetch_assoc($allsermon));
    mysql_free_result($allsermon);
    echo "</SERMON_INFO>";
    mysql_close($db);  

}

when i do this i get an error 500 and the page will not load.

The correct syntax is

echo "<URL><![CDATA[".$sermonrow['url']."]]></URL>";

But I'd rather you use an XML library like DOMDocument or SimpleXML to build your document, more complex, but you'll be less prone to simple errors like mixing up a closing tag name (see </NAME> ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM