uktutor Posted January 18, 2009 Share Posted January 18, 2009 Is there a better method of creating an XML document in php other than then one I’m using As you can see I’m explicitly converting all of the characters (I don’t think I’ve missed any) into characters that should be well formed in xml (character like “<” could make the xml non valid The method I’m using: header("Content-Type: text/xml"); mysql_connect("localhost", "/*username*/", "/*pass*/") or die(mysql_error()); mysql_select_db("/*db*/") or die(mysql_error()); $data2 = mysql_query("/*get relevant data from here*/")or die(mysql_error()); $i=0; print '<?xml version="1.0" encoding="UTF-8" ?>'; print '<a>'.$info2[/*columnName*/]; while (($info2 = mysql_fetch_array($data2)) && ($i < 10)){ print '<b>'.forXML($info2[/*columnName*/]).'</b>'; $i++;} print '</a>'; // a hidieous function to swap ‘non valid’ XML character function forXML($str){ $isXML = $str; //$isXML = str_replace(";", ";", $isXML); $isXML = str_replace("&", "&", $isXML); $isXML = str_replace("’", "'", $isXML); $isXML = str_replace("<", "<", $isXML);$isXML = str_replace(">", ">", $isXML); $isXML = str_replace("(", "(", $isXML);$isXML = str_replace(")", ")", $isXML); $isXML = str_replace("{", "{", $isXML);$isXML = str_replace("}", "}", $isXML); $isXML = str_replace("[", "[", $isXML);$isXML = str_replace("]", "]", $isXML); $isXML = str_replace("@", "@", $isXML);$isXML = str_replace("%", "%", $isXML); $isXML = str_replace("\\", "\", $isXML);$isXML = str_replace("\/", "/", $isXML);$isXML = str_replace("/", "/", $isXML); $isXML = str_replace("=", "=", $isXML);$isXML = str_replace("\'", "'", $isXML); $isXML = str_replace(":", ":", $isXML); $isXML = str_replace("=", "=", $isXML); $isXML = str_replace("\"", """, $isXML);$isXML = str_replace("\“", "“", $isXML); $isXML = str_replace("\”", "”", $isXML); $isXML = str_replace("\'", "'", $isXML);$isXML = str_replace("´", "´", $isXML); $isXML = str_replace("‘", "‘", $isXML);$isXML = str_replace("’", "’", $isXML); $isXML = str_replace("€", "€", $isXML); $isXML = str_replace("@", "@", $isXML); $isXML = str_replace("£", "£", $isXML); $isXML = str_replace("$", "$", $isXML); $isXML = str_replace("%", "~", $isXML); $isXML = str_replace("~", "%", $isXML); $isXML = str_replace("_", "_", $isXML);$isXML = str_replace("¯", "¯", $isXML); $isXML = str_replace("-", "-", $isXML);$isXML = str_replace("–", "–", $isXML); $isXML = str_replace("", "­", $isXML);$isXML = str_replace("—", "—", $isXML); $isXML = str_replace("°", "°", $isXML);$isXML = str_replace("˜", "˜", $isXML); $isXML = str_replace("~", "~", $isXML);$isXML = str_replace("!", "!", $isXML); $isXML = str_replace("|", "|", $isXML);$isXML = str_replace("¦", "¦", $isXML); $isXML = str_replace("?", "?", $isXML); return preg_replace("/[^A-Za-z0-9., \&#;]/","",$isXML) ; Quote Link to comment https://forums.phpfreaks.com/topic/141377-php-creating-valid-xml/ Share on other sites More sharing options...
DarkWater Posted January 18, 2009 Share Posted January 18, 2009 Just use htmlentities(). Quote Link to comment https://forums.phpfreaks.com/topic/141377-php-creating-valid-xml/#findComment-739998 Share on other sites More sharing options...
Mchl Posted January 18, 2009 Share Posted January 18, 2009 simplexml Quote Link to comment https://forums.phpfreaks.com/topic/141377-php-creating-valid-xml/#findComment-740009 Share on other sites More sharing options...
uktutor Posted January 18, 2009 Author Share Posted January 18, 2009 Php creating valid XML Just use htmlentities(). cool, almost, htmlentities converts lots of the chars, but I get a: XML Parsing Error: undefined entity for £ simplexml is an extension of php 5 (I'm using php 5). However, this file will need to go on many other sites. So one, it not only needs to be simple (people arent going to like more than one file, I've never used extensions before in php, but assume it requires additional files), and two, there is no guarantee the other servers will be up to php5 Quote Link to comment https://forums.phpfreaks.com/topic/141377-php-creating-valid-xml/#findComment-740016 Share on other sites More sharing options...
Mchl Posted January 18, 2009 Share Posted January 18, 2009 You're using an extension in the code you've posted. It's called mysql And PHP 4 is not supported by its creators for over a year now. Why should you support it? Quote Link to comment https://forums.phpfreaks.com/topic/141377-php-creating-valid-xml/#findComment-740026 Share on other sites More sharing options...
premiso Posted January 18, 2009 Share Posted January 18, 2009 Php creating valid XML Just use htmlentities(). cool, almost, htmlentities converts lots of the chars, but I get a: XML Parsing Error: undefined entity for £ simplexml is an extension of php 5 (I'm using php 5). However, this file will need to go on many other sites. So one, it not only needs to be simple (people arent going to like more than one file, I've never used extensions before in php, but assume it requires additional files), and two, there is no guarantee the other servers will be up to php5 Well you can always code for both. Do simplexml for those who have it/your script for those who do not. That way you are more functional =) I know it doesn't solve the problem, but yea. For the £ I am not sure, an easier method for your replacements instead of writing out all those lines you can have 2 arrays, make sure each index corresponds with each replacement and use that IE: $replace = array("&", "’"); $replaceWith = array("&", "'"); $isXML = str_replace($replace, $replaceWith, $isXML); That might be a bit easier to do than writing 200 lines of codes. However I think that you are going about this the wrong way, what is the right way, I have no clue. =) Quote Link to comment https://forums.phpfreaks.com/topic/141377-php-creating-valid-xml/#findComment-740031 Share on other sites More sharing options...
uktutor Posted January 18, 2009 Author Share Posted January 18, 2009 cool, I was going to use 2 arrays, but thought with so many entities, I would end up getting confused and not having them aligned :-\ . But I'll probably do as you've suggested. I have a very strong feeling I'm going about this the wrong way as well (that’s why I’m here ), php is a great language, there should be no reason for me to write out the hideous function above. Hmmm.. it sounds like simplexml() is what I should be using, but I just know I’m going to get hassled for not supporting lower specs. If I’m explicitly converting every char independently, there shouldn’t be any need to use simplexml() as well. I think you’ve both given directions to go away and think about, thanks. (Ill probably use : htmlentities(), and convert the unsupported entities back to xml format).. which just seems wrong to me, but I really think I need to support older php versions Quote Link to comment https://forums.phpfreaks.com/topic/141377-php-creating-valid-xml/#findComment-740045 Share on other sites More sharing options...
Mchl Posted January 18, 2009 Share Posted January 18, 2009 Hmmm.. it sounds like simplexml() is what I should be using, but I just know I’m going to get hassled for not supporting lower specs. I partly wish that someone discovered some lethal security issue in PHP4 that would force all hosting providers to move to PHP5 If anyone hassles you, tell them that everyone self-respecting software developing team is moving to PHP5 http://www.gophp5.org/ Quote Link to comment https://forums.phpfreaks.com/topic/141377-php-creating-valid-xml/#findComment-740053 Share on other sites More sharing options...
dropfaith Posted January 18, 2009 Share Posted January 18, 2009 I partly wish that someone discovered some lethal security issue in PHP4 that would force all hosting providers to move to PHP5 If anyone hassles you, tell them that everyone self-respecting software developing team is moving to PHP5 http://www.gophp5.org/ agreed coding for php 4 isnt your best bet always stay with the current high standard and you will resolve the issues of things not working alot faster in the future coding this to php 4 then having bugs in the future because its not supported properly in say php 6 would suck worse then telling your clients you use the highest standards available to date Quote Link to comment https://forums.phpfreaks.com/topic/141377-php-creating-valid-xml/#findComment-740062 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.