日期:2014-05-20  浏览次数:20942 次

jdom怎么取节点的属性名称啊???
jdom怎么取节点的属性名称啊???
比如:有一个这样的xml文件:
<root>
  <111>
  <222>haha</222>
  <333>tttt</333>
  </111>
</root>
我怎么取到222,333这两个属性名称啊!大家帮帮忙啊!谢谢!~~~~50分奉上!~~~~

------解决方案--------------------
222和333都不是属性,而是子节点
Java code

    Document doc = new Document();
    Element root = doc.getRootElement();
    Element element111 = root.getChild("111");
    List<Element> children = element111.getChildren();
    for (Element element : children) {
        String name = element.getName();//你可以遍历取得这些子节点的名称
        ...
    }

------解决方案--------------------
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class JxXml {
public JxXml(){
String xmlpath="你的文件.xml";
SAXBuilder builder=new SAXBuilder(false);
try {
Document doc=builder.build(xmlpath);
Element objExportExcel=doc.getRootElement();
Element objfields=objExportExcel.getChild("111");
List objfield=objfields.getChildren("field");
for (Iterator iter = objfield.iterator(); iter.hasNext();) {
Element field = (Element) iter.next();
String str222=field.getChildText("222");
String str333=field.getChildText("333");
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new JxXml();
}
}