比如解析下面这个config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Config>
<Common>
<AdapterType>1</AdapterType>
</Common>
<Adapters>
<Adapter name="SobeyVMSAdapter">
<ExternalReqPath>external/request</ExternalReqPath>
<InternalReqPath>internal/request</InternalReqPath>
</Adapter>
</Adapters>
</Config>
我们可以这样解析,该config.xml文件在项目的src目录下:
SAXReader reader = new SAXReader();
Document document = reader.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.xml"));
Element node = document.getRootElement();
Element element1 = node.element("Adapters");
Element element2 = element1.element("Adapter");
Element externalReqPath = element2.element("ExternalReqPath");
Element internalReqPath = element2.element("InternalReqPath");
Constant.ExternalReqPath = externalReqPath.getStringValue();
Constant.InternalReqPath = internalReqPath.getStringValue();
Constant.ExternalReqPath,Constant.InternalReqPath是常量字符串