标签:class nod throw 方式 document node 判断 attribute value
采用sax的方式来读取并解析xml文件。
public static void main(String[] args) throws DocumentException {
//1.获取到读取对象
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read("f:\\info.xml");
Element rootElement = doc.getRootElement();
getNodes(rootElement);
}
static public void getNodes(Element rootElement){
String name = rootElement.getName();
System.out.println("节点名称:"+name);
//获取节点属性
List<Attribute> attributes = rootElement.attributes();
for (Attribute attribute : attributes) {
System.out.println("属性名称:"+attribute.getName()+",属性value"+attribute.getValue());
}
String value=rootElement.getTextTrim();
if(!value.equals("")){
System.out.println("节点value"+value);
}
//判断是否有下一个节点
Iterator<Element> elementIterator = rootElement.elementIterator();
while (elementIterator.hasNext()) {
Element next = elementIterator.next();
getNodes(next);
}
}
标签:class nod throw 方式 document node 判断 attribute value
原文地址:https://www.cnblogs.com/hglSV/p/11192265.html