zoukankan      html  css  js  c++  java
  • 使用JAXB解析xml文件(一)

     

    1.java中解析xml的几种方式

    1.1 JDK原生dom形式 原理:一次性把xml读入内存,在内存中构建成树形结构。优点:对节点操作方便,缺点:需要大量的内存空间,浪费资源

    1.2 SAX形式 原理:基于事件形式,当解析器发现元素开始、元素结束、文本、文档的开始或结束等时,发送事件,程序员编写响应这些事件的代码,保存数据。优点:相对于dom形式更快捷,占用资源少。缺点:没有持久话。事件过后,若没保存数据,那么数据就丢了。

    1.3 DOM4J 它是一个开源的框架,具有性能优异、功能强大和极端易用使用的特点。

    1.4 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。

    2.JAXB的几个核心对象

    2.1 JAXBContext Jaxb的上下文,通过这个对象我们能拿到另外两个核心对象Unmarshaller(用于解析xml)和Marshaller(生成xml)

    2.2 JAXBContext通常使用它的静态方法newInstance(Class className)来获得对象

    2.3 Unmarshaller用于解析xml 通过JAXBContext的createUnmarshaller方法获得到

    2.4 Marshaller用于生成xml 通过JAXBContext的createMarshaller方法获得到

    3.示例代码

    3.1 实体类Bean与Property 

     1 package org.lyrk.accp8.s2.chapter.xml;
     2 
     3 import javax.xml.bind.annotation.*;
     4 
     5 /**
     6  * Created by niechen on 17/5/9.
     7  */
     8 @XmlRootElement
     9 @XmlAccessorType(XmlAccessType.FIELD)
    10 public class Bean {
    11 
    12     @XmlElement
    13     private Property property;
    14 
    15     public Property getProperty() {
    16         return property;
    17     }
    18 
    19     public void setProperty(Property property) {
    20         this.property = property;
    21     }
    22 }
    23 
    24 @XmlAccessorType(value = XmlAccessType.FIELD)
    25 class Property {
    26     @XmlAttribute(name = "id")
    27     private String id;
    28 
    29     @XmlAttribute(name = "value")
    30     private String value;
    31 
    32     public String getId() {
    33         return id;
    34     }
    35 
    36     public void setId(String id) {
    37         this.id = id;
    38     }
    39 
    40     public String getValue() {
    41         return value;
    42     }
    43 
    44     public void setValue(String value) {
    45         this.value = value;
    46     }
    47 }

    3.2 主函数类

     1 package org.lyrk.accp8.s2.chapter.xml;
     2 
     3 import javax.xml.bind.JAXBContext;
     4 import javax.xml.bind.JAXBException;
     5 import javax.xml.bind.Unmarshaller;
     6 import java.io.InputStream;
     7 
     8 /**
     9  * Created by niechen on 17/5/9.
    10  */
    11 public class Test {
    12 
    13     public static void main(String[] args) {
    14         try {
    15             JAXBContext jaxbContext = JAXBContext.newInstance(Bean.class);//创建JAXBContext对象,注意一定要传@XmlRootElement的所标记的类的类型
    16             Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();//拿到xml解析对象
    17             InputStream inputStream = Bean.class.getClassLoader().getResourceAsStream("bean.xml");//在classpath下读取xml的文件流
    18             Bean bean = (Bean) unmarshaller.unmarshal(inputStream);//将xml转换成实体对象
    19             System.out.println(bean.getProperty().getId());//输出ID属性
    20             System.out.println(bean.getProperty().getValue());//输出value属性
    21         } catch (JAXBException e) {
    22             e.printStackTrace();
    23         }
    24     }
    25 }

    3.3 xml文件内容

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <bean>
    3     <property id="name" value="10" />
    4 </bean>
  • 相关阅读:
    【 星 辰 · 第 二 条 约 定 】
    【 星 辰 · 第 一 条 约 定 】
    【 塔 · 第 三 条 约 定 】
    【 塔 · 第 二 条 约 定 】
    某些奇葩的正则校验
    localstorage本地存储的简单使用
    js基础(3)之闭包。
    js基础(2)filter()筛选过滤
    js基础(1)变量作用域
    scrum过程
  • 原文地址:https://www.cnblogs.com/niechen/p/6829499.html
Copyright © 2011-2022 走看看