java怎么通过xml节点的属性获取这个节点的值?
创建解析器
SAXReader saxreader = new SAXReader();读取文档
Document doc = saxreader.read(new File("url"));获取根
Element root = doc.getRootElement();获取子节点
List<Element> list = root.elements();System.out.println(e.elementText("name"));
System.out.println(e.element("score").attributeValue("java"));
Reader reader = new InputStreamReader(con
.getInputStream());
SAXReader sax = new SAXReader();
// sax.setEncoding("GBK");Document document = sax.read(reader);
document.setXMLEncoding("GBK");
Element root = document.getRootElement();
// Document doc = reader.read(read);
// Element root = doc.getRootElement();
readNode(root, "");public static void readNode(Element root, String prefix) {
if (root == null) return;
// 获取属性
List<Attribute> attrs = root.attributes();
if (attrs != null && attrs.size() > 0) {
System.err.print(prefix);
for (Attribute attr : attrs) {
System.err.print(attr.getValue() + " ");
}
System.err.println();
}// 获取他的子节点
List<Element> childNodes = root.elements();
prefix += "\t";
for (Element e : childNodes) {
//输出内容
System.err.println(e.getName()+":"+e.getData());
readNode(e, prefix);
}
}
创建解析器:
SAXReader saxreader = new SAXReader();读取文档:
Document doc = saxreader.read(new File("url"));获取根:
Element root = doc.getRootElement();
获取子节点:
List<Element> list = root.elements();
for(Element e : list){ System.out.println(e.elementText("name"));
System.out.println(e.element("score").attributeValue("java")); }。