You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB
Java

package com.docus.bgts.utils;
import com.docus.bgts.enums.Codes;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
public class XmlUtils {
//定义解析器和文档对象
private SAXReader saxReader;
private Document document;
public XmlUtils(String path) {
//获取解析器
saxReader = new SAXReader();
try {
//获取文档对象
document = saxReader.read(path);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public XmlUtils(InputStream path) {
//获取解析器
saxReader = new SAXReader();
try {
//获取文档对象
document = saxReader.read(path);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
*
* @param name
* @return
*/
public String getElementText(String name) {
//定位根节点
Element root = document.getRootElement();
//根据名称定位节点
Element msg = root.element(Codes.MSG.getMessage());
if(msg==null){
throw new RuntimeException("没有"+Codes.MSG.getMessage()+"节点");
}
// Element patInfo = msg.element(Codes.PAT_INFO.getMessage());
// if(patInfo==null){
// throw new RuntimeException("没有"+Codes.PAT_INFO.getMessage()+"节点");
// }
Element element = msg.element(name);
if(element==null){
return null;
}
//返回节点内容
return element.getText();
}
}