0

I am creating a XML file from a database using PHP..

I am following a syntax something like this

$query = "SELECT * FROM t1 ORDER BY field1";

$resultID = mysql_query($query, $linkID) or die("Data not found.");

$row = mysql_fetch_assoc($resultID);

$xml_output .="\t\t\t\t\t\t<given_name>"  . $row['fieldname'] . "</given_name>\n";

echo $xml_output

everything works well and almost a well formed XML is formed.. But there is a problem with some of the characters like &, ? etc., How can i overcome this?? I dont want to string replace these..

I want my XML to be off a special structure which needs to be validated against a specified schema..

Help appreciated..

9
  • And what problem is it exactly? Commented Jul 11, 2011 at 15:39
  • Error something like this : Value 'Morre?' is not facet-valid with respect to pattern '[\x00-/:->@-??-??-??-??-??-??-??-??-??-??-??-??-??-??-\v10ffff]*[\x00-\x08\x0b-\f\x0e-\x1f!-/:->@-??-??-??-??-??-??-??-??-??-??-??-??-??-??-\v10ffff][\x00-\x08\x0b-\f\x0e-\x1f!-/:->@-??-??-??-??-??-??-??-??-??-??-??-??-??-??-\v10ffff]*[\x00-/:->@-??-??-??-??-??-??-??-??-??-??-??-??-??-??-\v10ffff]*'. [Error] XML.xml:5747:32: cvc-type.3.1.3: The value 'Morre?' of element 'surname' is not valid. Commented Jul 11, 2011 at 15:42
  • 1
    It looks like you are trying to build XML documents by mashing together strings. Don't do that. Use an XML library. Commented Jul 11, 2011 at 15:42
  • And some other error like XML not well formed because of '&' Commented Jul 11, 2011 at 15:42
  • 3
    You should use DOMDocument to create the XML instead of doing it by hand. Commented Jul 11, 2011 at 15:42

2 Answers 2

3

Two solutions come up immediatly :

  1. Use <[[CDATA sections. No need in that case to escape special chars.
  2. Escape those chars. & is equals to &amp;. htmlspecialchars is indeed a PHP function to transform your data. But I'd rather go with the first solution.
Sign up to request clarification or add additional context in comments.

1 Comment

What if the data contains ]]>?
1

Use PHP's htmlspecialchars function to encode your special characters.

2 Comments

htmlentities will produce HTML entities. Most of them do not validate against most XML DTDs/Schemas (unless these define said entities). I think what you want here is htmlspecialchars.
You’re right, my bad. I think someone posted an answer with htmlentities, thus provoking my reply. I then reloaded the page to see if anything new happened and I pasted without thinking.

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.