使用Dom4j解析XML文件

public static String readXMl(){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
String s = null;
try{
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse("classpath:linuxFile.xml");
// s = document.getElementById("file").getNodeValue();
NodeList node = document.getElementsByTagName("temporary_file");
for (int i = 0;i<node.getLength();i++){
Element son = (Element)node.item(i);
for (Node node1 = son.getFirstChild(); node1 != null; node1 = node1.getNextSibling()){
if (node1.getNodeType() == Node.ELEMENT_NODE){
String name = node1.getNodeName();
String value = node1.getFirstChild().getNodeValue();
if(name.equals("filePath")){
s = value;
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}


return s;
}
<?xml version="1.0" encoding="UTF-8"?>
<temporary_file>
<filePath>E:ff</filePath>
</temporary_file>
原文地址:https://www.cnblogs.com/menbo/p/10537816.html