Java cultivation path-reading XML files and java cultivation path xml

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.