您的位置:首页 > 其它

根据结点获取xml文件中的部分数据

2016-12-22 15:12 489 查看
public class HhtParsePathXml {
private Logger logger = Logger.getLogger(getClass());

    

private String filePath;

private Document document;

public HhtParsePathXml(String filePath) {

this.filePath = filePath;
this.load(this.filePath);
}

private void load(String filePath){
File file = new File(filePath);
if (file.exists()) {
SAXReader saxReader = new SAXReader();
try {
document = saxReader.read(file);
} catch (DocumentException e) {

logger.error(e.getMessage());
           throw new RuntimeException(e);

}
} else{
logger.error("xml file not found");
throw new RuntimeException("xml file not found");

}
}

public String getXmlNode(String elementPath) {
if(document!=null)
{
return  document.selectSingleNode(elementPath).asXML();
}else
{
return null;
}

}

public Element getElementObject (String elementPath)
{
return (Element)document.selectSingleNode(elementPath);
}

public boolean isExist(String elementPath){
boolean flag = false;
Element element = this.getElementObject(elementPath);
if(element != null) flag = true;
return flag;
}

public String getElementText(String elementPath) {
Element element = this.getElementObject(elementPath);
if(element != null){
return element.getText().trim();
}else{
return null;
}
}

/**
* @param args
*/
public static void main(String[] args) {
HhtParsePathXml parseXml=new HhtParsePathXml("L2015080210612871.xml");
Element element=parseXml.getElementObject("/data/body");
System.out.println(element.asXML());

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐