Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从Android(XML)文档中的节点获取标签名称元素?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这样的 XML
<!--...-->
  <Cell X="4" Y="2" CellType="Magnet">
    <Direction>180</Direction>
    <SwitchOn>true</SwitchOn>
    <Color>-65536</Color>
  </Cell>
  <!--...-->

有很多Cell元素,我可以通过GetElementsByTagName获取Cell Nodes.但是,我意识到Node类没有GetElementsByTagName方法!如何在不通过ChildNodes列表的情况下从该单元节点获取Direction节点?我可以通过标记名来获取NodeList吗?

谢谢.

解决方法

您可以使用Element再次强制转换NodeList项,然后使用getElementsByTagName();来自Element类.
最好的方法是在项目中创建一个Cell对象,以及Direction,Switch,Color等字段.然后得到这样的数据.
String direction [];
NodeList cell = document.getElementsByTagName("Cell");
int length = cell.getLength();
direction = new String [length];
for (int i = 0; i < length; i++)
{
    Element element = (Element) cell.item(i);
    NodeList direction = element.getElementsByTagName("Direction");

    Element line = (Element) direction.item(0);

    direction [i] = getCharacterDatafromElement(linE);

    // remaining elements e.g Switch,Color if needed
}

你的getCharacterDatafromElement()将如下所示.

public static String getCharacterDatafromElement(Element E)
{
    Node child = e.getFirstChild();
    if (child instanceof CharacterData)
    {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return "";
}

大佬总结

以上是大佬教程为你收集整理的从Android(XML)文档中的节点获取标签名称元素?全部内容,希望文章能够帮你解决从Android(XML)文档中的节点获取标签名称元素?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。