Java cultivation path-reading XML files and java cultivation path xml
This time, we will record how to operate xml files in java. In fact, it is easy to get started with python crawler. In java, dom4j has a library similar to lxml in python.
It should be emphasized that when using the dom4j library, it actually has a dependency package, namely jaxen. If this parameter is not added, an error is returned. (The download links of dom4j and jaxen are sorted at the bottom)
Here we will mainly talk about how to use dom4j to read xml files (which can be loaded directly from the network or locally)
// Copy the file on the official website, directly loading the file public class Foo {public Document parse (URL url) throws into entexception {SAXReader reader = new SAXReader (); Document document = reader. read (url); return document ;}// load the local xmlpublic static Document parse (String path) {SAXReader reader = new SAXReader (); Document document = null; try {document = reader. read ("your xml file path");} catch (your entexception e) {// TODO Auto-generated catch block e. printStackTrace ();} return document ;}
Now, the Document class is a text that is parsed based on xml. Let's start from here.
<?xml version="1.0" encoding="UTF-8"?><note><to>George</to><from>John</from>
Here is a small example. The xml file provides
Public static void testDom4j (Document data) {// 1: Read the data Element lElement1 = (Element) file in. selectSingleNode ("/note/to"); String toValue = IElement1.getStringValue (); // each element in dom4j corresponds to a node. // 2: Read the attribute Element lElement2 = (Element) file of name in heading. selectObject ("/note/heading"); String headingNameValue = IElement2.attributeValue ("name") // 3 select based on Node attributes, select the Element lElement3 = (Element) file of type = "test2" from the body. selectObject ("/note/heading [@ type = 'test2'"); String bodValue = IElement3.getStringValue ();
}
These three methods should be quite common, and of course there are other APIs not mentioned. But they are all similar. You can visit the official website.
Write it here. Add more next time. (It's mainly too lazy to withdraw ~~)
Om4j:
Https://sourceforge.net/projects/dom4j/files/dom4j/1.6.1/
Jaxen's:
Http://maven.ibiblio.org/maven2/jaxen/jaxen/1.1.1/