PHP xmlreader到数组

时间:2010-12-03 19:22:50

标签: php xml xmlreader

以下测试函数使用PHP的xmlreader读取XML文件,并返回3个值的2维数组(“id”= key)。

如何编辑代码,以便在数组中返回用户名密钥和值? (显示在最后)

ini_set('always_populate_raw_post_data', 'on');

function test()
{
    $request = $HTTP_RAW_POST_DATA;
    error_reporting(E_ERROR | E_WARNING | E_PARSE);
    $url = "http://site.xml";
    $reader = new XMLReader();
    $reader->open($url);;

    $var = array();
    $i = 0;
    $limit = 3;

    while ($reader->read()) 
    {
        if ($reader->name == "id" && $reader->nodeType == XMLReader::ELEMENT)
        {
            if ($i == $limit) break;
            while ($reader->read())
            {
                if ($reader->nodeType == XMLReader::TEXT
                    || $reader->nodeType == XMLReader::CDATA
                    || $reader->nodeType == XMLReader::WHITESPACE
                    || $reader->nodeType == XMLReader::SIGNIFICANT_WHITESPACE)
                {
                    $var[$i]["id"] = $reader->value;
                }
                else if ($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == "id")
                {
                    break;
                }
             }
             $i++;
        }
    }
    $reader->close();

    echo '<pre>';
    print_r($var);
    echo '</pre>';
}

test()返回此数组

Array
(
    [0] => Array
        (
            [id] => 345
        )

    [1] => Array
        (
            [id] => 123
        )

    [2] => Array
        (
            [id] => 789
        )
)

您如何获得用户名密钥和值?

Array
(
    [0] => Array
        (
            [id] => 345
            [username] => name1
        )

    [1] => Array
        (
            [id] => 123
            [username] => name2
        )

    [2] => Array
        (
            [id] => 789
            [username] => name3       
        )
)

添加到Jose的回答

// needed to end element
else if ($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == "username")
{
    break;
}

2 个答案:

答案 0 :(得分:2)

这应该有用。

function test()
{
    $request = $HTTP_RAW_POST_DATA;
    error_reporting(E_ERROR | E_WARNING | E_PARSE);
    $url = "http://site.xml";
    $reader = new XMLReader();
    $reader->open($url);;

    $var = array();
    $i = 0;
    $limit = 3;

    while ($reader->read()) 
    {
        if (($reader->name == "id" || $reader->name == "username") && $reader->nodeType == XMLReader::ELEMENT)
        {
        $name = $reader->name;
            if ($i == $limit) break;
            while ($reader->read())
            {
                if ($reader->nodeType == XMLReader::TEXT
                    || $reader->nodeType == XMLReader::CDATA
                    || $reader->nodeType == XMLReader::WHITESPACE
                    || $reader->nodeType == XMLReader::SIGNIFICANT_WHITESPACE)
                {
                    $var[$i][$name] = $reader->value;
                }
                else if ($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == "id")
                {
                    break;
                }
             }

             if($name == "username")
        $i++;

        }
    }
    $reader->close();

    echo '<pre>';
    print_r($var);
    echo '</pre>';
}

<强>变化:

  

($ reader-&gt; name ==“id”||   $ reader-&gt; name ==“username”)

     

$ name = $ reader-&gt; name;

     

$ var [$ i] [$ name] = $ reader-&gt; value;

     

if($ name ==“username”)       $ I ++;

答案 1 :(得分:1)

if ($reader->name == "id" && $reader->nodeType == XMLReader::ELEMENT)

您需要让读者检查

$reader->name == "username"

现在你只是在读ID。您还必须从XML中读取用户名。一旦您还阅读了用户名,您也可以将其添加到您的阵列中。