XML解析
一、XML解析的准备工作
1.第一步,先将下面三个jar包复制导入到IDEA创建的lib文件中。
资源免费下载:dom4j-1.6.1.jar、jaxen-1.1-beta-7.jar、xstream-1.3.1.jar。
2.第二步,点击file打开Project Structure进行项目的一些配置。
3.第三步,打开Libraries,添加项目,再添加刚刚导入的三个jar文件包。
如此显示添加成功。
然后打开Modules,将src文件导入即可。
二、开始解析本地和在线xml文件
1.解析本地xml文件
将已写好xml文件保存在硬盘的一个位置,我将它保存在E盘
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();
}
}
输出结果:
books
1001
金苹果
锄禾日当午
-----------------------------
1002
银苹果
汗滴禾下土
-----------------------------
Process finished with exit code 0
当然如果你在解析本地文件的时候,出现了一下的情况,不要慌,主要原因是因为jdk的版本过高,我装的jdk-11.0.6版本。但完全不影响运行的结果,想要消除这种警告可以降低jdk版本,1.8版本就可以了。
2.解析网上xml文件
接下来我们来解析一个可以查询手机号码相关信息的网址。
地址为(http://apis.juhe.cn/mobile/get?%20phone=16670002013&dtype=xml&key=9f3923e8f87f1ea50ed4ec8c39cc9253)
然后将解析这个网站将手机号码里面的内容解析出来。
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("请输入正确的手机号码");
}
}
}
可以如下代码中修改手机号码的输入
String phone = "16670002013";
运行结果:
手机号码归属地为:湖南 张家界
号码的邮政编码为:0744
号码附属公司:联通
Process finished with exit code 0