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());
}
}
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());
}
}