1

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

无法确定如何递归地遍历 XML、检索父 ID 和记录子 ID 记录。问题是一些子 ID 也可以是父 ID。

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

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

我正在尝试使用 SimpleXML 来做到这一点,但是我无法弄清楚如何遍历孩子的孩子,这几乎看起来像。

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

4

3 回答 3

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);
于 2012-09-11T05:51:27.203 回答
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
        );
}

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

编辑:
很抱歉复制 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;
}
于 2012-09-11T05:29:58.313 回答
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: 
于 2012-09-11T07:56:42.740 回答