PHP xmlreader进行数组

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

您如何编辑代码,以便在数组中也返回用户名键和值? (显示在末尾)

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

解决方法:

这应该工作.

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->name == “id” ||
$reader->name == “username”)

$name = $reader->name;

$var[$i][$name] = $reader->value;

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

上一篇:c#-将XmlReader转换为XmlTextReader


下一篇:php – 为什么这个xmlreader代码不起作用?