XML解析。我怎样才能得到孩子的孩子?
问题描述:
我试图解析这个XML在Java中:XML解析。我怎样才能得到孩子的孩子?
<entities>
<entity name="product_section" id="1">
<product_type>3</product_type>
<section_type>1</section_type>
<name>Empresa</name>
<description>d</description>
<position>1</position>
<align>left</align>
<files section_id="1">
<ico id="ico_1" type="normal" src="sections/1/icons/ico.png"></ico>
<ico id="ico_2" type="hover" src="sections/1/icons/ico.png"></ico>
<ico id="ico_3" type="active" src="sections/1/icons/ico.png"></ico>
<img id="img_1" type="normal" src="sections/1/img/pestanya.png"></img>
<img id="img_2" type="hover" src="sections/1/img/pestanya-hover.png"></img>
<img id="img_3" type="active" src="sections/1/img/pestanya-active.png"></img>
<background id="background_1" type="background" position="1" src="sections/1/background/bg1.png"></background>
<background id="background_2" type="background" position="2" src="sections/1/background/bg2.png"></background>
<background id="background_3" type="background" position="3" src="sections/1/background/bg3.png"></background>
</files>
</entity>
但我就是通过实体实现的循环,让所有实体,并<product_type>
,<section_type>
等 但我想循环通过文件。
这是我的执行至今:
try {
File contingut = new File("xmlfile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(contingut);
doc.getDocumentElement().normalize();
System.out.println("root of xml file " + doc.getDocumentElement().getNodeName());
//loop a cada entity
NodeList nodes = doc.getElementsByTagName("entity");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
Element element = (Element) node;
System.out.println("product_type: " + getValue("product_type", element));
System.out.println("section_type: " + getValue("section_type", element));
System.out.println("name: " + getValue("name", element));
System.out.println("description: " + getValue("description", element));
System.out.println("position: " + getValue("position", element));
System.out.println("align: " + getValue("align", element));
}
} catch (Exception e){
e.printStackTrace();
}
getValue
功能是:
private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
我已经做了很多的谷歌搜索的,而我看到的是“简单”的例子,有父母和孩子,但不是孩子的孩子。
任何帮助,将不胜感激。
答
在第一个建议:
if (element.getNodeType() == Element.ELEMENT_NODE) { // do smth}
,并回答你的问题:
您可以
这Element element = (Element) node;
使用此代码或这样的事情后
检查元素类型只需重写你的代码。在你创建element
后,你可以使用element.getChildNodes();
它可以得到所有的子标签。之后,你写简单的循环,你从节点列表中得到每个节点元素,如下所示:
NodeList nodes = element.getChildNodes();
for(int i =0; i < nodes.getLength(); i++){
Element child = (Element) nodes.item(i);
if(child.getNodeType() == Element.ELEMENT_NODE){
String tagName = child.getTagName();
if(!tagName.equals("files")){
System.out.println(tagName + " : " + child.getTextContent());
}else{
NodeList filesChilds = child.getChildNodes();
for(int j = 0; j < filesChilds.getLength(); j++){
//and like above
}
}
}
}
那么是什么问题?对于文件标记,您需要编写另一个getValue函数 – 2013-04-05 09:33:39
您并未使用android,但在android开发页面上,有一个非常简洁的pullparser工作原理示例。也许值得检查一下,因为它在普通的java中是一样的。它在这里http://developer.android.com/training/basics/network-ops/xml.html – WereWolfBoy 2013-04-05 09:35:37