从Android(XML)文档中的节点获取标签名称元素?

从Android(XML)文档中的节点获取标签名称元素?,第1张

概述我有这样的 XML: <!--...--> <Cell X="4" Y="2" CellType="Magnet"> <Direction>180</Direction> <SwitchOn>true</SwitchOn> <Color>-65536</Color> </Cell> <!--...--> 有很多Cell元素,我可以通过GetElementsByT 我有这样的 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)文档中的节点获取标签名称元素?所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1133430.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存