zoukankan      html  css  js  c++  java
  • 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基本语法

        

          

  • 相关阅读:
    PHP学习笔记十二【数组排序】
    PHP学习笔记十一【数组】
    PHP学习笔记十【数组】
    PHP学习笔记九【数组二】
    PHP学习笔记八【数组】
    Codeforces 612E
    Codeforces 616E
    codeforce #339(div2)C Peter and Snow Blower
    poj 1113 Mall
    poj 2187 Beauty Contest
  • 原文地址:https://www.cnblogs.com/DeepBlues/p/2992369.html
Copyright © 2011-2022 走看看