How to parse XML in IDEA

1. Preparations for XML parsing

1. The first step is to copy and import the following three jar packages into the lib file created by IDEA.

Insert picture description here
Resources are free to download: dom4j-1.6.1.jar , jaxen-1.1-beta-7.jar , xstream-1.3.1.jar .
Insert picture description here

2. In the second step, click file to open the Project Structure for some configuration of the project.

Insert picture description here

3. The third step is to open Libraries, add the project, and add the three jar file packages just imported.

Insert picture description here
This shows that the addition is successful.
Insert picture description here
Then open Modules and import the src file.
Insert picture description here

2. Start parsing local and online xml files

1. Parse the local xml file

Save the written xml file in a location on the hard disk, and I will save it on the E drive
Insert picture description here

Insert picture description here

package Jishou_college.XML解析;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class Demo1_xml本地文件解析 {
    
    
    public static void main(String[] args) throws IOException, DocumentException {
    
    
        //1、获取输入流-xml文件
        FileInputStream fis = new FileInputStream("e://Demo1.xml");
        //2、创建xml读取对象
        SAXReader sr = new SAXReader();
        //3、读取并得到文档对象
        Document doc = sr.read(fis);
        //4、通过文档获取根元素
        Element root = doc.getRootElement();
        //5、开始解析元素
        System.out.println(root.getName());
        //操作一波
        //Element book = root.element("book");
        //Element name = book.element("name");
        //System.out.println(name.getText());
        List<Element> es = root.elements();
        for (int i = 0; i < es.size(); i++) {
    
    
            Element book = es.get(i);
            System.out.println(book.attributeValue("id"));
            System.out.println(book.elementText("name"));
            System.out.println(book.elementText("info"));
            System.out.println("-----------------------------");
        }
        //关闭文件流
        fis.close();
    }
}

Output result:

books
1001
金苹果
锄禾日当午
-----------------------------
1002
银苹果
汗滴禾下土
-----------------------------

Process finished with exit code 0

Of course, if something happens when you are parsing local files, don't panic. The main reason is that the jdk version is too high. I installed the jdk-11.0.6 version. But it does not affect the results of the operation at all. If you want to eliminate this warning, you can reduce the jdk version, version 1.8 is fine.
Insert picture description here

2. Parse the online xml file

Next, we will parse a URL that can query information about mobile phone numbers.
The address is (http://apis.juhe.cn/mobile/get?%20phone=16670002013&dtype=xml&key=9f3923e8f87f1ea50ed4ec8c39cc9253)
Insert picture description here
Then the website will be parsed to parse out the content in the mobile phone number.

Insert picture description here

package Jishou_college.XML解析;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Demo2_网络文件解析 {
    
    
    public static void main(String[] args) throws IOException, DocumentException {
    
    
        //1、获取到xml资源的输入流
        String phone = "16670002013";
        URL url = new URL("http://apis.juhe.cn/mobile/get?%20phone=" + phone + "&dtype=xml&key=9f3923e8f87f1ea50ed4ec8c39cc9253");
        //打开链接
        URLConnection conn = url.openConnection();
        //拿下网址的输入流
        InputStream is = conn.getInputStream();
        //2、创建一盒XML读取对象
        SAXReader sr = new SAXReader();
        //3、通过读取对象 读取xml数据吗,并返回文档对象
        Document doc = sr.read(is);
        //4、获取根节点
        Element root = doc.getRootElement();
        //5、解析内容
        String code = root.elementText("resultcode");
        //如果code不是200,就不是在查询号码,程序会报错。
        if ("200".equals(code)) {
    
    
            Element result = root.element("result");
            String province = result.elementText("province");
            String city = result.elementText("city");
            String areacode = result.elementText("areacode");
            String company = result.elementText("company");
            //内层if是为了筛选当城市和号码归属地一样的时候
            if (province.equals(city)) {
    
    
                System.out.println("手机号码归属地为:" + city);
                System.out.println("号码的邮政编码为:" + areacode);
                System.out.println("号码附属公司:" + company);
            } else {
    
    
                System.out.println("手机号码归属地为:" + province + " " + city);
                System.out.println("号码的邮政编码为:" + areacode);
                System.out.println("号码附属公司:" + company);
            }
        } else {
    
    
            System.out.println("请输入正确的手机号码");
        }
    }


}

You can modify the input of the mobile phone number in the following code

String phone = "16670002013";

operation result:

手机号码归属地为:湖南 张家界
号码的邮政编码为:0744
号码附属公司:联通

Process finished with exit code 0


Insert picture description here

                                                                         How IDEA debugs

                                                     A very convenient testing method-JUnit unit testing (IDEA)


Guess you like

Origin blog.csdn.net/mjh1667002013/article/details/114780839