将while循环生成的xml分配给变量

时间:2010-06-24 08:25:51

标签: php mysql xml

我使用while循环创建XML,但现在我需要在生成的XML前面加上XML标头信息和包装标签,但我很难让它工作,这是我的代码,

$result = mysql_query("SELECT * FROM users")
or die(mysql_error());

$row = mysql_fetch_array($result);

    while ($row = mysql_fetch_array($result)) {
        $pumaXML  = "<userDetails>";
        $pumaXML .= "<userID>".$row['uid']."</userID>";
        $pumaXML .= "<userName>".$row['userName']."</userName>";
        $pumaXML .= "<points>".$row['points']."</points>";
        $pumaXML .= "<imageURL>".$row['imageURL']."</imageURL>";
        $pumaXML .= "<thumbURL>".$row['thumbURL']."</thumbURL>";
        $pumaXML .= "</userDetails>";
    };

我似乎无法找到一种方法来做到这一点,我也尝试过制作一个函数,但这并没有那么顺利,这就是代码,

function createXML($result) {
    while ($row = mysql_fetch_array($result)) {
        $pumaXML  = "<userDetails>";
        $pumaXML .= "<userID>".$row['uid']."</userID>";
        $pumaXML .= "<userName>".$row['userName']."</userName>";
        $pumaXML .= "<points>".$row['points']."</points>";
        $pumaXML .= "<imageURL>".$row['imageURL']."</imageURL>";
        $pumaXML .= "<thumbURL>".$row['thumbURL']."</thumbURL>";
        $pumaXML .= "</userDetails>";
    };
    return $pumaXML;
};

提前Thanx!

1 个答案:

答案 0 :(得分:2)

以下是使用DOM

的方法
function createUserDetailsXml(array $result) {

    $dom  = new DOMDocument;
    $dom->formatOutput = TRUE; // enable automatic indenting
    $dom->loadXML('<users/>'); // set root node

    foreach($result as $row) {

        // create user-details node
        $user = $dom->createElement('user-details');

        // create and append details to user-details node
        $user->appendChild(
            $dom->createElement('user-id', $row['uid']));
        $user->appendChild(
            $dom->createElement('user-name', $row['userName']));
        $user->appendChild(
            $dom->createElement('user-points', $row['points']));
        $user->appendChild(
            $dom->createElement('image-url', $row['imageURL']));
        $user->appendChild(
            $dom->createElement('thumb-url', $row['thumbURL']));

        // add user-details node to XML document, e.g. users node
        $dom->documentElement->appendChild($user);
    };
    return $dom->saveXML(); // returns the formatted XML
};

请注意,该函数希望您传入完整的结果数组,因此我可以使用:

进行测试
$result = array(
    array(
        'uid'      => 1,
        'userName' => 'Gordon',
        'points'   => PHP_INT_MAX,
        'imageURL' => 'http://example.com/gordon.jpg',
        'thumbURL' => 'http://example.com/t_gordon.jpg'
    ),
    array(
        'uid'      => 2,
        'userName' => 'John <blink>"Frigging"</blink> Doe',
        'points'   => 0,
        'imageURL' => 'http://example.com/johndoe.jpg',
        'thumbURL' => 'http://example.com/t_johndoe.jpg'
    )
);
echo createUserDetailsXml($result);

然后该函数将返回

<?xml version="1.0"?>
<users>
  <user-details>
    <user-id>1</user-id>
    <user-name>Gordon</user-name>
    <user-points>2147483647</user-points>
    <image-url>http://example.com/gordon.jpg</image-url>
    <thumb-url>http://example.com/t_gordon.jpg</thumb-url>
  </user-details>
  <user-details>
    <user-id>2</user-id>
    <user-name>John &lt;blink&gt;"Frigging"&lt;/blink&gt; Doe</user-name>
    <user-points>0</user-points>
    <image-url>http://example.com/johndoe.jpg</image-url>
    <thumb-url>http://example.com/t_johndoe.jpg</thumb-url>
  </user-details>
</users>

请注意,DOM会自动为您丢弃John Doe名字中的特殊字符。 DOM还将确保XML元素名称(或者如果使用它们的属性)在语法上有效。它还添加了XML Prolog。