• 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基本语法

        

          

  • 相关阅读:
    [转]lftp的致命错误:证书验证:不信任
    github每次push都需要密码以及用户名的解决办法
    Fedora最小化安装后没有ifconfig命令
    [转载]MySql常用命令总结
    chrome浏览器强制采用https加密链接
    红帽系列linux自行配置本地yum源
    linux 下dd命令直接清除分区表(不用再fdisk一个一个的删除啦)
    linux分区工具fdisk的使用
    Java多线程实现......(1,继承Thread类)
    第一篇文章--我为什么要写博客?
  • 原文地址:https://www.cnblogs.com/DeepBlues/p/2992369.html
Copyright © 2020-2023  润新知