[Java] 纯文本查看 复制代码
public class XMLParseUtils {
public static void main(String[] args) {
try {
Element rootNode = readXMLRoot("src\\DpnI5.xml");
Element foundNode=null;
Element hit_def = getNode(rootNode, "Hit_def", foundNode);
System.out.println(hit_def.getName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 根据文件路径获取xml根元素节点
*
* @param filePath 文件路径
* @return
* @throws Exception
*/
public static Element readXMLRoot(String filePath) throws Exception {
// 创建saxReader对象
SAXReader reader = new SAXReader();
// 通过read方法读取一个文件 转换成Document对象
Document document = reader.read(new File(filePath));
// 获取根节点元素对象
Element node = document.getRootElement();
return node;
}
/**
* 查询节点
* @param node 要查询数据的节点即根节点
* @param nodeString 要查询的节点的名字
* @param nodeFound 要查询的节点。传null即可
* @return
*/
public static Element getNode(Element node,String nodeString,Element nodeFound){
if (nodeFound!=null){
return nodeFound;
}else{
// 当前节点下面子节点迭代器
Iterator<Element> it = node.elementIterator();
while((it.hasNext())&&(nodeFound==null)){
Element e= it.next();
if (e.getName().equals(nodeString)){
nodeFound=e;
}else{
nodeFound=getNode(e,nodeString,nodeFound);
}
}
}
return nodeFound;
}
}