开发者

Building XML with PHP - Performance in mind

开发者 https://www.devze.com 2022-12-27 05:18 出处:网络
When building XML in PHP, is it quicker to build a string, then echo out the string or to use the XML functions that php gives you? Currently I\'m doing the following:

When building XML in PHP, is it quicker to build a string, then echo out the string or to use the XML functions that php gives you? Currently I'm doing the following:

UPDATED to better code snippet:

$searchParam = mysql_real_escape_string($_POST['s']);
$search = new Search($searchParam);

if($search->retResult()>0){
    $xmlRes = $search->buildXML();
}
else {
    $xmlRes = '<status>no results</status>';
}

$xml = "<?xml version=\"1.0\"?>";
$xml.="<results>";
$xml.=$xmlRes;
$xml.="</results>"

header ("content-type: text/xml");
header ("content-length: ".strlen($xml));
echo($xml);


class Search {
private $num;
private $q;

function __construct($s){
    $this->q = mysql_query('select * from foo_table where name = "'.$开发者_如何学Pythons.'"');
    $this->num = mysql_num_rows($this->q);
}

function retResult(){
    return $this->num;
}

function buildXML(){
    $xml ='<status>success</status>';
    $xml.='<items>';
    while($row = mysql_fetch_object($this->q)){
        $xml.='<item>';
        $desTag = '<info><![CDATA[';
        foreach ($row as $key => $current){
            if($key=='fob'){
                //do something with current
                $b = mysql_query('select blah from dddd where id ='.$current);
                $a = mysql_fetch_array($b);
                $xml.='<'.$key.'>'.$a['blah'].'</'.$key.'>';
            }
            else if($key =='this' || $key=='that'){
                $desTag = ' '.$current;
            }
            else {
                $xml.='<'.$key.'>'.$current.'</'.$key.'>';
            }   
        }
        $desTag.= ']]></info>';
        $xml.=$desTag;
        $xml.='</item>';
    }
    $xml.='</items>';
    return $xml;
}

}

Is there a faster way of building the xml? I get to about 2000 items and it starts to slow down..

Thanks in advance!


Use the xml parser. Remember when you concatenate a string, you have to reallocate the WHOLE STRING on every concatenation.

For small strings, string is is probably faster, but in your case definitely use the XML functions.


I don't see that you're making no attempt to escape the text before concatenating it. Which means that sooner or later you're going to generate something that is almost-but-not-quite XML, and which will be rejected by any conforming parser.

Use a library (XMLWriter is probably more performant than others, but I haven't done XML with PHP).


You have a SQL query inside of a loop, which is usually quite a bad idea. Even if each query takes half a millisecond to complete, it's still a whole second just to execute those 2000 queries.

What you need to do is post the two queries in a new question so that someone can show you how to turn them into a single query using a JOIN.

Database stuff usually largely outweighs any kind of micro-optimization. Whether you use string concatenation or XMLWriter doesn't matter when you're executing several thousand queries.


try to echo in each iteration (put the echo $xml before the while loop ends, and reset $xml at the beggining), should be quicker


That code snippet doesn't make a lot of sense, please post some actual code, reduced for readability.

A faster version of the code you posted would be

$xml = '';
while ($row =  mysql_fetch_row($result))
{
    $xml .= '<items><test>' . implode('</test><test>', $row) . '</test></items>';
}

In general, using mysql_fetch_object() is slightly slower than the other options.

Perhaps what you were trying to do was something like this:

$xml = '<items>';
while ($row =  mysql_fetch_assoc($result))
{
    $xml .= '<item>';
    foreach ($row as $k => $v)
    {
        $xml .= '<' . $k . '>' . htmlspecialchars($v) . '</' . $v . '>';
    }
    $xml .= '</item>';
}
$xml .= '</items>';

As mentionned elsewhere, you have to escape the values unless you're 100% sure there will never be any special character such as "<" ">" or "&". This also applies to $k actually. In that kind of script, it is also generally more performant to use XML attributes instead of nodes.

With so little information about your goal, all we can do is micro-optimize. Perhaps you should work on the principles behind your script instead? For instance, do you really have to generate 2000 items. Can you cache the result, can you cache anything? Can't you paginate the result, etc...

Quick word about using PHP's XML libraries, XMLWriter will generally be slightly slower than using string manipulation. Everything else will be noticeably slower than strings.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号