*# 什么是解析XML?
相信一些初学者回经常听到反射,解析xml文件一类的词汇,但是究竟什么是解析什么是反射呢?
解析xmlwen文件
解析(parser):从xml中把我们需要的数据提取出来就是xml解析.
解析的分类:
- JDK自带方式
* Dom解析
* SAX解析 - 第三方解析
* Dom4J
* JDOM
在xml中,最终所有的标签都跟顶级标签有关系的
DOM: document Object Model 文档对象模型(树状模型) :通过面向对象的方式把我们所需要的诗句提取出来就叫做Dom解析.
框架都是用dom4j解析的.
Rss阅读器也是通过解析就可以轻松实现的哦!!!!!
DOM4J特点: 性能优越,功能强大,及其易使用
给大家找到了一个关于SAX解析和DOM解析的比较,有兴趣的朋友可以看一下。
简单介绍完解析的分类接下来给大家讲解一下如何解析一个xml文件
解析web.xml案例
为了方便解析,我将写好的web.xml移动到src下(web.xml的内容是随便在其他练习工程中随便拷贝的)
然后进入jar包:dom4j-1.6.1.jar(我用到的jar包版本,点击打开后第一个
链接: link.)
接下来给大家详细解释一下如何解析我们的web.xml
package com.ambow.parser;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ParserXml {
public static void main(String[] args) throws Exception {
// 创建读取流,将我们的目标文件读取出来
InputStream in=ParserXml.class.getClassLoader().getResourceAsStream("web.xml");
// 创建读取器
SAXReader reader=new SAXReader();
// 用读取器中获取读取的对象
Document document=reader.read(in);
// 获取根节点(所有的解析xml,都需要先获取根节点,即根标签)
Element root=document.getRootElement();
// 获取根标签的名字和内容
String rootname=root.getName();
// 打印一下看看我们能否获取跟标签
System.out.println(rootname);
System.out.println("------------------------------");
// Element servlet=root.element("servlet");
// 过去跟标签后,我们继续向下寻找跟标签的子标签(爹(跟标签)找儿子(子标签))
// 跟标签下有且有多个servlet和servlet-mapping标签,所以用集合
List<Element> list=root.elements("servlet");
List<Element> list1=root.elements("servlet-mapping");
// 遍历读取标签中我们想要的信息
for (Element e : list) {
System.out.println(e.elementText("servlet-name"));
System.out.println(e.elementText("servlet-class"));
}
System.out.println("----------------------------------------");
// 遍历servlet-mapper标签的信息
for (Element e1 : list1) {
System.out.println(e1.elementText("servlet-name"));
System.out.println(e1.elementText("url-pattern"));
}
}
}
接下来是web.xml中的内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>DoSaveCheckUserAction</servlet-name>
<servlet-class>com.ambow.action.DoSaveCheckUserAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoSaveCheckUserAction</servlet-name>
<url-pattern>/dosavecheckuser</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DoLoginUserAction</servlet-name>
<servlet-class>com.ambow.action.DoLoginUserAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoLoginUserAction</servlet-name>
<url-pattern>/dologin</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DoCheckUnameAction</servlet-name>
<servlet-class>com.ambow.action.DoCheckUnameAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoCheckUnameAction</servlet-name>
<url-pattern>/checkuname</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DoCheckRegAction</servlet-name>
<servlet-class>com.ambow.action.DoCheckRegAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoCheckRegAction</servlet-name>
<url-pattern>/checkreg</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoCheckRegAction1</display-name>
<servlet-name>DoCheckRegAction1</servlet-name>
<servlet-class>com.ambow.action.DoCheckRegAction1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoCheckRegAction1</servlet-name>
<url-pattern>/docheck</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoFindLoginInfoAction</display-name>
<servlet-name>DoFindLoginInfoAction</servlet-name>
<servlet-class>com.ambow.action.DoFindLoginInfoAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoFindLoginInfoAction</servlet-name>
<url-pattern>/findlast</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoSavePubmessageAction</display-name>
<servlet-name>DoSavePubmessageAction</servlet-name>
<servlet-class>com.ambow.action.DoSavePubmessageAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoSavePubmessageAction</servlet-name>
<url-pattern>/savepubmessage</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoJoinPubAction</display-name>
<servlet-name>DoJoinPubAction</servlet-name>
<servlet-class>com.ambow.action.DoJoinPubAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoJoinPubAction</servlet-name>
<url-pattern>/joinpub</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoFindUserLikeAction</display-name>
<servlet-name>DoFindUserLikeAction</servlet-name>
<servlet-class>com.ambow.action.DoFindUserLikeAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoFindUserLikeAction</servlet-name>
<url-pattern>/finduserlike</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>yanzhengma</servlet-name>
<servlet-class>com.ambow.action.YanZhengMa</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>yanzhengma</servlet-name>
<url-pattern>/yanzhengma</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DoFindFriendAction</servlet-name>
<servlet-class>com.ambow.action.DoFindFriendAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoFindFriendAction</servlet-name>
<url-pattern>/findfriendbyid</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoDeleteFriendAction</display-name>
<servlet-name>DoDeleteFriendAction</servlet-name>
<servlet-class>com.ambow.action.DoDeleteFriendAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoDeleteFriendAction</servlet-name>
<url-pattern>/deletefriend</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoSaveFriendAction</display-name>
<servlet-name>DoSaveFriendAction</servlet-name>
<servlet-class>com.ambow.action.DoSaveFriendAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoSaveFriendAction</servlet-name>
<url-pattern>/savefriend</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoCheckFriendAction</display-name>
<servlet-name>DoCheckFriendAction</servlet-name>
<servlet-class>com.ambow.action.DoCheckFriendAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoCheckFriendAction</servlet-name>
<url-pattern>/checkfriend</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoCheckSuccess</display-name>
<servlet-name>DoCheckSuccess</servlet-name>
<servlet-class>com.ambow.action.DoCheckSuccess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoCheckSuccess</servlet-name>
<url-pattern>/checksuccess</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoFindSmessageAction</display-name>
<servlet-name>DoFindSmessageAction</servlet-name>
<servlet-class>com.ambow.action.DoFindSmessageAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoFindSmessageAction</servlet-name>
<url-pattern>/findsmessage</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoSaveSmessageAction</display-name>
<servlet-name>DoSaveSmessageAction</servlet-name>
<servlet-class>com.ambow.action.DoSaveSmessageAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoSaveSmessageAction</servlet-name>
<url-pattern>/savesmessage</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoFindManager</display-name>
<servlet-name>DoFindManager</servlet-name>
<servlet-class>com.ambow.action.DoFindManager</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoFindManager</servlet-name>
<url-pattern>/findmanager</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoupUserAction</display-name>
<servlet-name>DoupUserAction</servlet-name>
<servlet-class>com.ambow.action.DoupUserAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoupUserAction</servlet-name>
<url-pattern>/upuser</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DoFindPubmessageAction</display-name>
<servlet-name>DoFindPubmessageAction</servlet-name>
<servlet-class>com.ambow.action.DoFindPubmessageAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DoFindPubmessageAction</servlet-name>
<url-pattern>/findmessage</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RefreshAction</display-name>
<servlet-name>RefreshAction</servlet-name>
<servlet-class>com.ambow.action.RefreshAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RefreshAction</servlet-name>
<url-pattern>/RefreshAction</url-pattern>
</servlet-mapping>
</web-app>
运行代码,结果如下:
以上就是解析web.xml的步骤啦,觉得有帮助的朋友可以点个赞。