Jump to content

Recommended Posts

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(";", "&#59;", $isXML);
$isXML = str_replace("&", "&", $isXML);
$isXML = str_replace("&#8217;", "'", $isXML);
$isXML = str_replace("<", "<", $isXML);$isXML = str_replace(">", ">", $isXML);
$isXML = str_replace("(", "&#40;", $isXML);$isXML = str_replace(")", "&#41;", $isXML);
$isXML = str_replace("{", "&#123;", $isXML);$isXML = str_replace("}", "&#125;", $isXML);
$isXML = str_replace("[", "&#91;", $isXML);$isXML = str_replace("]", "&#93;", $isXML);
$isXML = str_replace("@", "&#64;", $isXML);$isXML = str_replace("%", "&#37;", $isXML);
$isXML = str_replace("\\", "&#92;", $isXML);$isXML = str_replace("\/", "&#47;", $isXML);$isXML = str_replace("/", "&#47;", $isXML);
$isXML = str_replace("=", "&#61;", $isXML);$isXML = str_replace("\'", "&#39;", $isXML);	
$isXML = str_replace(":", "&#58;", $isXML); $isXML = str_replace("=", "&#61;", $isXML);
$isXML = str_replace("\"", """, $isXML);$isXML = str_replace("\“", "&#147;", $isXML);
$isXML = str_replace("\”", "&#148;", $isXML);   
$isXML = str_replace("\'", "'", $isXML);$isXML = str_replace("&#180;", "´", $isXML);
$isXML = str_replace("&#145;", "‘", $isXML);$isXML = str_replace("&#146;", "’", $isXML);	
$isXML = str_replace("€", "&#128;", $isXML); $isXML = str_replace("@", "&#64;", $isXML); 	
$isXML = str_replace("£", "&#163;", $isXML); $isXML = str_replace("$", "&#36;", $isXML); 	
$isXML = str_replace("%", "&#126;", $isXML); $isXML = str_replace("~", "&#37;", $isXML);
$isXML = str_replace("_", "&#95;", $isXML);$isXML = str_replace("¯", "&#175;", $isXML);
$isXML = str_replace("-", "&#45;", $isXML);$isXML = str_replace("–", "&#150;", $isXML);
$isXML = str_replace("­", "&#173;", $isXML);$isXML = str_replace("—", "&#151;", $isXML);
$isXML = str_replace("°", "&#176;", $isXML);$isXML = str_replace("˜", "&#152;", $isXML);
$isXML = str_replace("~", "&#126;", $isXML);$isXML = str_replace("!", "&#33;", $isXML);
$isXML = str_replace("|", "&#124;", $isXML);$isXML = str_replace("¦", "&#166;", $isXML);
$isXML = str_replace("?", "&#63;", $isXML);	
return preg_replace("/[^A-Za-z0-9., \&#;]/","",$isXML) ;

Link to comment
https://forums.phpfreaks.com/topic/141377-php-creating-valid-xml/
Share on other sites

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

 

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("&", "&#8217");
$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. =)

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  :D ), 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

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/

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.