public class XMLUtils {
private static SAXReader reader = new SAXReader();
private static Document dom = null;
public static Document load(String fileName) {
InputStream in = PropertiesHandler.class.getClassLoader()
.getResourceAsStream("properties/" + fileName);
try {
dom = reader.read(in);
} catch (DocumentException e) {
e.printStackTrace();
}
return dom;
}
public static void main(String[] args) {
Document dom = load("pushInfo.xml");
String content = dom.getRootElement().element("sourceGoodsSMS").element("content").getText();
String detail = MessageFormat.format(content,
"苹果",
"小五"
);
System.out.println(detail);
}
properties/pushInfo.xml
<?xml version="1.0" encoding="UTF-8"?
>
<!-- 推送货源给车辆时短信格式 -->
<info>
<sourceGoodsSMS>
<content>
飞驿网为您推送一条货源信息:货源名称:{0},联系人:{1};(详情请登录飞驿网http://www.56gate.com)
</content>
</sourceGoodsSMS>
</info>
打印:飞驿网为您推送一条货源信息:货源名称:苹果,联系人:小五;(详情请登录飞驿网http://www.56gate.com)
}