JAVA解析XML

XML解析技术有两种 DOM SAX

  • DOM方式
    • 根据XML的层级结构在内存中分配一个树形结构,把XML的标签,属性和文本等元素都封装成树的节点对象
    • 优点: 便于实现增 删 改 查
    • 缺点: XML文件过大可能造成内存溢出
  • SAX方式
    采用事件驱动模型边读边解析:从上到下一行行解析,解析到某一元素, 调用相应解析方法

  • 优点: 不会造成内存溢出,

  • 缺点: 查询不方便,但不能实现 增 删 改

不同的公司和组织提供了针对DOM和SAX两种方式的解析器
- SUN的jaxp
- Dom4j组织的dom4j(最常用:如Spring)
- JDom组织的jdom

JAXP 解析

JAXP是JavaSE的一部分,在javax.xml.parsers包下,分别针对dom与sax提供了如下解析器:

  • Dom
    • DocumentBuilder
    • DocumentBuilderFactory
  • SAX
    • SAXParser
    • SAXParserFactory

示例XML如下,下面我们会使用JAXP对他进行增 删 改 查操作

  • config.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans SYSTEM "constraint.dtd">
<beans>
    <bean id="id1" class="com.fq.domain.Bean">
        <property name="isUsed" value="true"/>
    </bean>
    <bean id="id2" class="com.fq.domain.ComplexBean">
        <property name="refBean" ref="id1"/>
    </bean>
</beans>
  • constraint.dtd
<!ELEMENT beans (bean*) >
        <!ELEMENT bean (property*)>
        <!ATTLIST bean
                id CDATA #REQUIRED
                class CDATA #REQUIRED
                >

        <!ELEMENT property EMPTY>
        <!ATTLIST property
                name CDATA #REQUIRED
                value CDATA #IMPLIED
                ref CDATA #IMPLIED>

JAXP-Dom

/**
 * Author:林万新 lwx
 * Date:  2017/11/16
 * Time: 10:57
 */
public class XmlRead {
    @Test
    public void client() throws ParserConfigurationException, IOException, SAXException {
        //生成一个DOMM解析器
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        //解析XM文件:加上这句话就可以把xml文件放在resources文件下;如果不放就要放在项目目录下;
        Document document = builder.parse(ClassLoader.getSystemResourceAsStream("config.xml"));

    }
}

DocumentBuilder的parse(String/File/InputSource/InputStream param)方法可以将一个XML文件解析为一个Document对象,代表整个文档.
Document(org.w3c.dom包下)是一个接口,其父接口为Node, Node的其他子接口还有Element Attr Text等.

  • Node

Node常用方法 释义
Node appendChild(Node newChild) Adds the node newChild to the end of the list of children of this node.
Node removeChild(Node oldChild) Removes the child node indicated by oldChild from the list of children, and returns it.
NodeList getChildNodes() A NodeList that contains all children of this node.
NamedNodeMap getAttributes() A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.
String getTextContent() This attribute returns the text content of this node and its descendants.

  • Document

Dom查询

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值