发布网友 发布时间:2022-04-25 17:52
共4个回答
热心网友 时间:2023-10-22 21:30
package test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
/**
* 使用JDOM实现XML解析,jar包:jdom-2.0.5.jar
* @author zhurongfu
*
*/
public class JdomTest {
public static void parseXml (File file) throws JDOMException, IOException{
SAXBuilder sax = new SAXBuilder();
Document doc = sax.build(file);
Element root = doc.getRootElement();
parseElement(root);
}
private static void parseElement(Element element) {
// TODO Auto-generated method stub
List<Element> elementList = element.getChildren();
for(Element e:elementList){
if(e.getChildren().size() > 0){
parseElement(e);
}else {
String elementName = e.getName();
if("id".equals(elementName)){
System.out.println("a的值为:"+e.getValue());
}else if("destaddr".equals(elementName)) {
System.out.println("b的值为:"+e.getValue());
}else if("status".equals(elementName)){
System.out.println("c的值为:"+e.getValue());
}
}
}
}
public static void main(String[] args) throws JDOMException, IOException {
// TODO Auto-generated method stub
File file = new File("test.xml");
parseXml(file);
}
}
热心网友 时间:2023-10-22 21:31
自带的dom就可以
热心网友 时间:2023-10-22 21:31
SAX,DOM用这两种解析方法都可以,可以去网上载一个工具包
热心网友 时间:2023-10-22 21:32
用dom4j按节点去找,很亏搞定!