JAXB解析----详解

  (1)什么是XML与Java类映射

  XML数据与JAVA类之间的对应关系,就是一种映射。

  (2)JAXB的工作原理示意图。

*XML转换到Java对象的过程又叫做unmarshal

*java对象转换成XML的过程叫marshal

  (3)示例一:Java对象转化成XML(marshal)

   Article.java

View Code
package TestFor0331;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Article {
    private String title;
    private String autor;
    private String email;
    private String date;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAutor() {
        return autor;
    }
    public void setAutor(String autor) {
        this.autor = autor;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
}

   DemoForMarshal

View Code
package TestFor0331;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class DemoForMarshal {
    public static void main(String[] args) {
        File xmlFile=new File("test3.xml");
        JAXBContext context;
        
        try {
            context=JAXBContext.newInstance(Article.class);
            Marshaller m=context.createMarshaller();
            Article article=new Article();
            article.setAutor("Janet");
            article.setDate("20080801");
            article.setEmail("will@163.com");
            article.setTitle("XML");
            
            m.marshal(article, xmlFile);
            
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        
        
    }

}

结果:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<article>
    <autor>Janet</autor>
    <date>20080801</date>
    <email>will@163.com</email>
    <title>XML</title>
</article>

  (4)示例二:XML转化成Java对象(unmarsal) 

    DemoForUnmarshal.java  

View Code
package TestFor0331;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class DemoForUnmarshal {

    public static void main(String[] args) {
        File file=new File("test3.xml");
        
        JAXBContext context;
        
        try {
            context=JAXBContext.newInstance(Article.class);
            Unmarshaller u=context.createUnmarshaller();
            Article article=(Article)u.unmarshal(file);
            
            System.out.println(article.getAuthor());
            System.out.println(article.getDate());
            System.out.println(article.getTitle());
            System.out.println(article.getEmail());
            
            
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        

    }

}

  Xml部分用的示例一生成的xml文件

运行结果:

Janet
20080801
XML
will@163.com

  (5)示例三:XML到Java类复杂示例
  Articles.java

View Code
package TestFor0331;

public class Article {
    
    private String title;
    private String author;
    private String email;
    private String date;
    
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
}

  DemoForUnmarshalMore.java

View Code
package TestFor0331;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "articles")
public class DemoForUnmarshalMore {
    
    List<Article>article=new ArrayList<Article>();
    
    public List<Article> getArticle() {
        return article;
    }

    public void setArticle(List<Article> article) {
        this.article = article;
    }

    public static void main(String[] args) {
        File xmlFile =new File("article.xml");
        
        JAXBContext context;
        
        try {
            //注意此处是DemoForUnmarshalMore.class不是Article.java类
            context=JAXBContext.newInstance(DemoForUnmarshalMore.class);
            
            Unmarshaller u=context.createUnmarshaller();
            
            DemoForUnmarshalMore unmarshalMore=
                (DemoForUnmarshalMore)u.unmarshal(xmlFile);
            
            List<Article>art=unmarshalMore.getArticle();
            
            for(Article a:art){
                
                System.out.println(a.getAuthor());
                System.out.println(a.getDate());
                System.out.println(a.getEmail());
                System.out.println(a.getTitle());
                
                
            }
            
            
            
            
        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }

}

  articles.xml

View Code
<?xml version="1.0" encoding="UTF-8" ?>
<articles>
    <article>
        <title>XML概述</title>
        <author>janet</author>
        <email>winfu@163.com</email>
        <date>20080801</date>
    </article >
        
    <article>
        <title>Java基本语法</title>
        <author>janet</author>
        <email>winfu@163.com</email>
        <date>20080801</date>
    </article>
</articles>

运行结果如下:

janet
20080801
winfu@163.com
XML概述
janet
20080801
winfu@163.com
Java基本语法

 

    

      

 

转载于:https://www.cnblogs.com/DeepBlues/archive/2013/03/31/2992369.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值