PHP - 如何从XML中读取子项的递归子项并正确显示它?

时间:2012-09-11 05:14:48

标签: php xml recursion simplexml children

我正在连接到一个为我提供用户列表的系统,数据将以“树格式”显示,这样您就可以看到父/子关系,它几乎就像一个经销商树。< / p>

无法弄清楚如何递归遍历XML,检索父ID并记录子ID记录。问题是一些子ID也可以是父ID。

我也不确定如何从XML直接访问某个UserID,因为每条记录只包含属性。

我已将XML的示例副本上传到以下网址: http://www.fluffyduck.com.au/sampleXML.txt

我正在尝试使用SimpleXML,但是我无法弄清楚如何通过它看起来像孩子的孩子。

任何帮助表示赞赏!因为这让我完全难过。

3 个答案:

答案 0 :(得分:1)

这项工作并返回一个147 SimpleXMLElement数组(可用于数组)。

$content = file_get_contents('http://www.fluffyduck.com.au/sampleXML.txt');
$xml = new SimpleXMLElement($content);
$users = myUserReader($xml->user);

function myUserReader($node)
{
    $users = array ();
    $users[] = $node->attributes();
    foreach ($node as $user)
    {
        $users[] = $user->attributes();
        if (isset($user->user))
        {
            $users = array_merge($users, myUserReader($user->user));
        }
    }
    return $users;
}

// displays 147 children
var_dump($users);

// displays 147
echo count($users);

答案 1 :(得分:0)

$xml = new SimpleXMLElement($xml_str);
$user = $xml->user;

while($curr_user = $curr_user->children('user'))
{
    // do some code
    $attr = array(
        $curr_user['id'],
        $curr_user['username'],
        $curr_user['firstname'],
        // ...etc
        );
}

因此,它只是使用和查找子节点的示例。要遍历所有数组,您可以像这段代码一样使用递归函数。

编辑:
Sry复制Ninsuo代码,但它是完美的:)

$content = file_get_contents('http://www.fluffyduck.com.au/sampleXML.txt');
$xml = new SimpleXMLElement($content);
$users = myUserReader($xml->user);

function myUserReader($node,$str)
{
    $users = array ();
    $users[] = $node->attributes();
    foreach ($node as $user)
    {
        $users[] = $user->attributes();
        if (isset($user->user))
        {
            $str .= '<ul>'.$user['fisrt_name'];
            $users = array_merge($users, myUserReader($user->user));
            $str .= '</ul>';
        }
        else $str .= '<li>'.$user['fisrt_name'].'</li>';
    }
    return $users,$str;
}

答案 2 :(得分:0)

要获得一系列父母/子女,您可以使用:

<?php

$content = file_get_contents('http://www.fluffyduck.com.au/sampleXML.txt');
$xml = new SimpleXMLElement($content);

$users = array ();
myUserReader($xml->user, $users);

function myUserReader($node, &$users)
{
    if (array_key_exists("{$node['id']}", $users) === false)
    {
        $users["{$node['id']}"] = array ();
    }
    if (isset($node->user))
    {
        for ($key = 0; ($key < $node->user->count()); $key++)
        {
            $user = $node->user[$key];
            if (!in_array("{$node['id']}", $users["{$node['id']}"]))
            {
                $users["{$node['id']}"][] = "{$user['id']}";
            }
            myUserReader($user, $users);
        }
    }
}

foreach ($users as $parent => $children)
{
    echo "{$parent}: " . implode(", ", $children) . "\n";
}
?>

这将输出:

41: 61, 62, 69, 89, 127, 204, 323, 340, 341
61: 
62: 64, 78, 80, 116, 120
64: 66
66: 67
67: 68, 114, 161, 162, 166, 203
68: 70
70: 72, 77, 104, 118, 135, 138, 143, 154, 156, 171, 172, 173, 174
72: 73, 86, 163, 165, 167
73: 75
75: 
86: 92, 103, 108, 112, 117, 119, 121, 124, 125, 137, 144
92: 107, 110, 111, 142
107: 
110: 
111: 
142: 
103: 
108: 
112: 
117: 
119: 
121: 
124: 
125: 
137: 
144: 
163: 
165: 
167: 
77: 79, 113
79: 
113: 
104: 106
106: 
118: 
135: 
138: 
143: 
154: 
156: 
171: 
172: 
173: 
174: 
114: 
161: 
162: 
166: 169, 175, 176, 178, 179, 180, 183, 184, 205, 206
169: 
175: 
176: 
178: 
179: 
180: 
183: 
184: 
205: 
206: 
203: 
78: 136
136: 
80: 101
101: 
116: 122, 126, 129, 130, 131, 133, 139, 140, 141
122: 
126: 
129: 
130: 
131: 
133: 
139: 
140: 
141: 
120: 145, 146, 147, 148, 149, 150, 151, 157, 160, 168
145: 
146: 
147: 
148: 
149: 
150: 152, 153, 155, 159
152: 
153: 
155: 
159: 
151: 
157: 
160: 
168: 
69: 
89: 
127: 
204: 207
207: 208, 209, 210, 211, 212, 227, 254, 300
208: 213, 214, 215, 226, 228, 234, 255, 294, 295, 297, 299, 320
213: 
214: 
215: 
226: 
228: 
234: 
255: 274, 275
274: 
275: 
294: 
295: 
297: 
299: 
320: 
209: 
210: 217, 218, 222
217: 
218: 
222: 
211: 
212: 
227: 
254: 
300: 
323: 
340: 
341: