There is a configuration file that needs to parse out the root node and child nodes, as well as the information of the attributes under the child nodes. Although the w3c.dom specification can be parsed, we try to parse it using dom4j.
The jar package involved has dom4j-1.6.1.jar
Xml configuration file:
<?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="computer" class="org.crazyit.app.main.Computer"> <property name="name" value="SUN Wukong's smart phone" /> <property name="out" ref="betterPrinter" /> </bean> <bean id="printer" class="org.crazyit.app.inter.impl.Printer" /> <bean id="betterPrinter" class="org.crazyit.app.inter.impl.BetterPrinter" /> <bean id="now" class="java.util.Date" scope="prototype" /> </beans>
Parse the java class of the xml configuration file:
SAXReader reader = new SAXReader();
Document doc = reader.read(new File(filePath));
/ / Get the root node
Element root = doc.getRootElement();
/ / Traverse the child nodes under the root node (also can traverse a child node with child nodes)
for(Object obj : root.elements()) {
Element ele = (Element) obj;
/ / Get the properties of the child node
String beanId = ele.attributeValue("id");
String beanClazz = ele.attributeValue("class");
String beanScope = ele.attributeValue("scope");
}
Parse the string in xml format:
Map map = new HashMap();
StringReader reader = new StringReader(msg);
InputSource source = new InputSource(reader);
SAXReader sr = new SAXReader();
Document doc = sr.read(source);
/ / After getting the Document, the following steps are consistent with the steps above to parse the xml file
But if it is a general type: such as html configuration file (that is, not this Spring xml), the parsing syntax is different, such as:
Map map = new HashMap();
Document doc = null;
try {
Doc = DocumentHelper.parseText(xml); // Convert string to XML
Element rootElt = doc.getRootElement(); // Get the root node
System.out.println("root node:" + rootElt.getName()); // get the name of the root node
Iterator iter = rootElt.elementIterator("head"); // Get the child node head under the root node
// traverse the head node
while (iter.hasNext()) {
Element recordEle = (Element) iter.next();
String title = recordEle.elementTextTrim("title"); // Get the child node title value under the head node
System.out.println("title:" + title);
map.put("title", title);
Iterator iters = recordEle.elementIterator("script"); // Get the child node script under the child node head
// Traverse the Response node under the Header node (continue to traverse if there are child nodes under the child node)
Iterator iterss = rootElt.elementIterator("body"); ///Get the child node under the root node
/ / Traverse the body node
while (iterss.hasNext()) {
Element recordEless = (Element) iterss.next();
String result = recordEless.elementTextTrim("result"); // Get the child node result value under the body node
System.out.println("result:" + result);
Iterator itersElIterator = recordEless.elementIterator("form"); // Get the child nodes from the child body from
/ / Traverse the from node under the body node
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return map;
}