• 09_使用jaxb完成对象和xml的转换


    处理XML的方式:

      SAX

      dom4j

      Xstream-->stax

      XStream,Jackson

      JAXB:

    JAXB实例:

    package com.bling.xml;
    
    import java.io.StringReader;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    import org.junit.Test;
    
    public class TestJaxb {
        @Test
        public void test01(){
            try {
                JAXBContext ctx = JAXBContext.newInstance(Student.class);
                Marshaller marshaller = ctx.createMarshaller();
                Student stu = new Student(1,"杨明亮",21,new Classroom(1,"计算机",2014));
                marshaller.marshal(stu, System.out);
            } catch (JAXBException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        @Test
        public void test02(){
            String xml ="<?xml version="1.0" encoding="UTF-8" standalone="yes"?><student><age>21</age><classroom><grade>2014</grade><id>1</id><name>计算机</name></classroom><id>1</id><name>杨明亮</name></student>";
            JAXBContext ctx;
            try {
                ctx = JAXBContext.newInstance(Student.class);
                Unmarshaller um = ctx.createUnmarshaller();
                Student stu = (Student)um.unmarshal(new StringReader(xml));
                System.out.println("student name:"+stu.getName());
            } catch (JAXBException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    }

    GitHub源码地址:https://github.com/WebServcie/04_java_xml

  • 相关阅读:
    Django URL 命名空间
    数据库
    iOS之网络数据下载和JSON解析
    IOS之AFNetworking的使用
    IOS之网络数据下载和JSON解析
    IOS之XML解析
    iOS网络基础知识
    iOS开发--Block
    IOS开发——使用数据库
    IOS 开发下拉刷新和上拉加载更多
  • 原文地址:https://www.cnblogs.com/yangml/p/3866716.html
Copyright © 2020-2023  润新知