将while循环生成的xml分配给变量 [英] Assigning xml generated by a while loop to a variable

查看:80
本文介绍了将while循环生成的xml分配给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating XML using a while loop, but now I need to prepend and append the generated XML with the XML header info and wrapper tag, but I am struggling to get it to work, here is my code,

$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>";
    };

我似乎找不到解决方法,我也尝试创建一个函数,但是效果并不理想,这里是代码,

I can't seem to find a way to do this, I also tried making a function, but that didn't go that well, here is that code,

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;
};

先谢谢!

推荐答案

以下是使用 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
};

请注意,该函数希望您传递完整的结果数组,因此我可以使用以下方法进行测试:

Note that the function expects you to pass in the full result array, so I could test it with:

$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.

Please notice that DOM escaped the special chars in John Doe's name for you automatically. DOM will also make sure the XML element names (or attributes if you use them) are syntactically valid. It also added the XML Prolog.

这篇关于将while循环生成的xml分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆