public class XmlManager {
public static String getConstant(String name) {
Document doc = getDocument();
// XPath语句,选取属性name值为name变量的值的constant标签
String xpath = "//constant[@name='" + name + "']";
Element con = (Element) doc.selectSingleNode(xpath);
if (con != null) {
return con.attributeValue("value");
} else {
return null;
}
}
private static Document getDocument() {
// 创建解析器
SAXReader reader = new SAXReader();
// 加载配置文件
InputStream is = ConfigurationManager.class.getResourceAsStream(“path”);//path改你自己的xml实际路径
Document doc = null;
try {
doc = reader.read(is);
} catch (DocumentException e) {
e.printStackTrace();
throw new RuntimeException("加载配置文件出错");
}
return doc;
}
}
java读取xml文件
最新推荐文章于 2022-03-17 09:25:19 发布