0

i have a error when running this code .. i run fine when there is no special characteres in database. if its has special characters then i got the error please solve me i am very thankful.

Following Error are occured when any special charachers in database like " ' & ? " i don't why those error come .. and i am not using DOM or XMLWrite just simple create the xml via file, and clerify 1 thing that CDDATA also not working for my code i check it. Please tell me some thing how could i make the xml with error less..

following are the code:

    $file= fopen("../xml/{$productID}.xml" , "w"); 


    $_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
    $_XML = "<!DOCTYPE Server SYSTEM \"opt/pdos/etc/pdoslrd.dtd\">";

    $_xml .="<productsnode>";


     while ($row = mysql_fetch_array($productQuery, MYSQL_ASSOC)) { 

        $_xml .=" <product>";
        $_xml .="   <productID>" . $row['productID'] . "</productID>";
        $_xml .="   <productName>" . htmlspecialchars($row['productName']) . "</productName>";          
        $_xml .="   <productDescription>" . htmlspecialchars($row['productDescription']) . "</productDescription>"; 
        $_xml .="   <productPicture>" . htmlspecialchars($row['productPic']) . "</productPicture>";
        $_xml .=" <category>";
        $_xml .="   <categoryID>" . $row['categoryID'] . "</categoryID>";
        $_xml .="   <categoryName>" . htmlspecialchars($row['categoryName']) . "</categoryName>";   
        $_xml .="   <categoryDescription>" . htmlspecialchars($row['categoryDiscription']) . "</categoryDescription>";  
        $_xml .="   <categoryPicture>" . htmlspecialchars($row['categoryPic']) . "</categoryPicture>";              
        $_xml .=" <subCategory>";
        $_xml .="   <subCategoryID>" . $row['subCategoryID'] . "</subCategoryID>";
        $_xml .="   <subCategoryName>" . htmlspecialchars($row['subCategoryName']) . "</subCategoryName>";  
        $_xml .="   <subCategoryDetail>" . htmlspecialchars($row['subCategoryDescription']) . "</subCategoryDetail>";               
        $_xml .=" </subCategory>";
        $_xml .=" </category>";
        $_xml .=" </product>";

    }
    $_xml .="</productsnode>";
    fwrite($file, $_xml);
    fclose($file);
9
  • 2
    What error ? Don't be an ignorant. ;) Commented Mar 21, 2011 at 11:10
  • 1
    possible duplicate of Assigning xml generated by a while loop to a variable. In other words, use DOM or XMLWriter. Commented Mar 21, 2011 at 11:16
  • Did you run the query SET NAMES 'utf8'? Commented Mar 21, 2011 at 11:22
  • @Gordon, did you read the question or just the title? Commented Mar 21, 2011 at 11:24
  • @jswolf both. I've glanced over the example code and noticed that it's using string concatenation and understood OP is having issues with special chars, which bears the question why s/he's not using a more proper tool for these things, e.g. DOM or XMLWriter. And the linked dup shows an example how to use that. Besides, it's rather pointless using htmlspecialchars when working with XML (!= HTML). Commented Mar 21, 2011 at 11:33

4 Answers 4

2

wrap your XML in CDATA

<![CDATA[your content here]]>

(This will also take care of specialchars which are not replaced by htmlspecialchars())

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

2 Comments

how please tell me .. <![CDATA[your content here]]>
for example your third line in the while loop would look like this: $_xml .=" <productName><![CDATA[" . htmlspecialchars($row['productName']) . "]]></productName>";
1

Special chars are illegal in xml so you need to wrap everything between CDATA tags

eg:

$_xml .="   <subCategoryName><![CDATA[" . htmlspecialchars($row['subCategoryName']) . "]]></subCategoryName>"; 

read more at -> http://www.w3schools.com/xml/xml_cdata.asp

your code should be something like:

$_xml .=" <product>";
        $_xml .="   <productID><![CDATA[" . $row['productID'] . "]]></productID>";
        $_xml .="   <productName><![CDATA[" . htmlspecialchars($row['productName']) . "]]></productName>";          
        $_xml .="   <productDescription><![CDATA[" . htmlspecialchars($row['productDescription']) . "]]></productDescription>"; 
        $_xml .="   <productPicture><![CDATA[" . htmlspecialchars($row['productPic']) . "]]></productPicture>";
        $_xml .=" <category>";
        $_xml .="   <categoryID><![CDATA[" . $row['categoryID'] . "]]></categoryID>";
        $_xml .="   <categoryName><![CDATA[" . htmlspecialchars($row['categoryName']) . "]]></categoryName>";   
        $_xml .="   <categoryDescription><![CDATA[" . htmlspecialchars($row['categoryDiscription']) . "]]></categoryDescription>";  
        $_xml .="   <categoryPicture><![CDATA[" . htmlspecialchars($row['categoryPic']) . "]]></categoryPicture>";              
        $_xml .=" <subCategory>";
        $_xml .="   <subCategoryID><![CDATA[" . $row['subCategoryID'] . "]]></subCategoryID>";
        $_xml .="   <subCategoryName><![CDATA[" . htmlspecialchars($row['subCategoryName']) . "]]></subCategoryName>";  
        $_xml .="   <subCategoryDetail><![CDATA[" . htmlspecialchars($row['subCategoryDescription']) . "]]></subCategoryDetail>";               
        $_xml .=" </subCategory>";
        $_xml .=" </category>";
        $_xml .=" </product>";

If that doesn't work it means there is something else, I suggest you copy the error you get and put it in your answer and maybe take a screenshot of what is stored in the database. If you want help give us more details

5 Comments

problem not solve ... i will get error on the same... :( ' will make error
@Appz if you are not going to use DOM or XMLWriter, how about at least updating your question with the actual error message then? You have been asked to do that already in the comments to your question.
i update and i am not using the DOM or XMLWriter if its good and solve my problem then kindly tell me how could i integrate to my that code DOM / XMLWriter
@Appz Venture: why did you mark my answer as accepted and then removed it ....? and did you only do that with the row I pasted as an example? because you have to do this everywhere
i specify you answer correct but i not actually correct please correct the answer so i leave it as correct .. i know i should facing the difficulties but what can i do i got too much error .. please help me error is for special characters or could u tell me is any alternative that it write the xml correct format and i could change in the Admin panel CMS code.. when entering the data into the database .. i simple insert the record now
0

It is VERY very difficult to tell when you're not posting the exact errors. However, I will try to do an avid guess: check encodings. It's likely that you're using latin-swedish (AKA-ISO 8859-1) on the database and utf8 for the rest... or viceversa. I would recommend using utf8 for everything and saving your time.

Check table encodings, then the connection encoding (mysql_* functions by default could do the connection using ISO-8859-1 and you should use http://php.net/manual/en/function.mysql-set-charset.php in case that is happening), and that should help.

ALSO: Why are you concatenating strings for generating XML? It is a much better idea to do it with proper XML libraries like SimpleXML (please notice there's other libraries, you can check XMLWriter and many others too). Links: http://mx.php.net/manual/en/book.simplexml.php and ... I can't write the other link because I'm a newb in here :) Google is everyone's friend.

If you go to PEAR, there's more stuff.

Comments

0

I am not really answering your question exactly, but perhaps you should take a look at mysql xml abilities. This may depend upon your mysql version, and especially if you could possibly use the CLI methods to access your sql.

The article mentioned does describe the slightly more convoluted means of getting xml via sql selects though which worked for me on 5.0.67 (win32).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.