Luk*_* Vo 6 xml android xml-parsing
我有这样的XML:
<!--...-->
<Cell X="4" Y="2" CellType="Magnet">
<Direction>180</Direction>
<SwitchOn>true</SwitchOn>
<Color>-65536</Color>
</Cell>
<!--...-->
Run Code Online (Sandbox Code Playgroud)
有很多Cell elements
,我可以获得Cell Nodes GetElementsByTagName
.但是,我意识到Node
班级没有GetElementsByTagName
方法!如何Direction
从该单元节点获取节点,而无需查看列表ChildNodes
?我可以NodeList
从Document
班级中获取标签名称吗?
谢谢.
Adi*_*mro 14
您可以NodeList
再次使用该项目Element
,然后getElementsByTagName();
在Element
课堂中使用.最好的办法是让Cell Object
你的项目非常久远像场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
}
Run Code Online (Sandbox Code Playgroud)
你getCharacterDataFromElement()
将如何遵循.
public static String getCharacterDataFromElement(Element e)
{
Node child = e.getFirstChild();
if (child instanceof CharacterData)
{
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12482 次 |
最近记录: |