简体   繁体   English

需要帮助获得子节点

[英]Need help getting child nodes

im reading my XML and looping through the child nodes when i move it to another server its stops working, any ideas on why it would stop.. 当我将其移动到另一台服务器时,我正在读取我的XML并遍历子节点,它停止工作,以及关于为什么停止的任何想法。

here is the XML: 这是XML:

<masterxmlholder>   
    <userinfo login="user1">
    <rde-idea:lastlogin code="0">Wed Dec 31 17:00:00 MST 1969</rde-idea:lastlogin>
    </userinfo>

    <userinfo login="user2">
    <rde-idea:lastlogin code="4564568522258">Wed Dec 31 17:00:00 MST 1969</rde-idea:lastlogin>
    </userinfo>
</masterxmlholder>

and here is the loop i'm trying to get to the child with 这是我想带孩子去的循环

foreach ($xml->children() as $user) {
    echo '<tr><td> Name :  </td><td>' . $user['login'] . $user->lastlogin '</td><td>';

}
echo '</table></center>';

Now I can get the name attribute to display from each of the child nodes, however I can NOT get any of the child's inner nodes to display 现在,我可以从每个子节点中获取要显示的name属性,但是我无法获得要显示的所有子内部节点

this is all trimmed down to give you the idea what im trying to do .. but its basically to see how im trying to access the child nodes and perhaps point out my flaw. 所有这些内容都经过精简,以使您了解我正在尝试执行的操作..但基本上可以查看我如何尝试访问子节点并指出我的缺点。

thanks for reading. 谢谢阅读。

The lastlogin info is actually a child of the userinfo node, so you will need to use the children() method again to retrieve the info. lastlogin信息实际上是userinfo节点的子项,因此您将需要再次使用children()方法来检索该信息。 Note that in this example, I removed the XML namespace so that I could initialize $xml 请注意,在此示例中,我删除了XML名称空间,以便可以初始化$xml

$xmlstring = <<<XML
<masterxmlholder>   
    <userinfo login="user1">
    <lastlogin code="0">Wed Dec 31 17:00:00 MST 1969</lastlogin>
    </userinfo>

    <userinfo login="user2">
    <lastlogin code="4564568522258">Wed Dec 31 17:00:00 MST 1969</lastlogin>
    </userinfo>
</masterxmlholder>
XML;

$xml = new SimpleXMLElement($xmlstring);
echo '<center><table>';

foreach ($xml->children() as $user) {
    $name = $user['login'];
    foreach ($user->children() as $child ) {
       $lastlogin = $child;
    }
    echo '<tr><td> Name :  </td><td>' . $name . $lastlogin . '</td></tr>';
}
echo '</table></center>';

I tested the code here: http://writecodeonline.com/php/ 我在这里测试了代码: http : //writecodeonline.com/php/

Side note, I would not use the center tag, as that is mixing presentation with content, which is frowned upon. 旁注,我不会使用center标签,因为这是将演示文稿与内容混合在一起的方法,这是令人讨厌的。 Consider using CSS to style and position your table. 考虑使用CSS设置表格的样式和位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM