java解析复杂的xml⽂件_Java解析复杂xml⽂件,使⽤Xpath Java解析复杂xml⽂件,需要使⽤到xpath,⾸先使⽤了Jdom2。代码⽰例如下:
public static Document GetFileFromLocal(String filename){
try {
FileInputStream stream=new FileInputStream(String.format("d:/x下载/%s.xml", filename));
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = saxBuilder.build(stream);
return doc;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
java xml是什么}
return null;
}
但是该xml解析器,不⽀持xpath。所以修改为dom4j。在l⽂件⾥⾯添加依赖,maven会⾃动下载该jar⽂件:
org.dom4j
dom4j
2.1.0
解析xml代码⽰例如下:
public class XmlHelper {
public static Document parse(String url) {
SAXReader reader = new SAXReader();
Document document = null;
try {
document = ad(url);
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
public static Element getRootElement(String url){
SAXReader reader = new SAXReader();
Document document = null;
try {
document = ad(url);
} catch (DocumentException e) {
e.printStackTrace();
}
RootElement();
}
public static Element getXmlElement(Element rootNode, String xpath) { if(rootNode==null||xpath==""){
return null;
}
return rootNode.element(xpath);
}
public static String getXmlNodeText(Element rootNode, String xpath) { if(rootNode==null||xpath==""){
return null;
}
Element element= rootNode.element(xpath);
if(element==null){
return "";
}
Text();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。