java object与xml的转换

概述:以前对于XML与Java对象的转换了解比较少,今天学微信接口API时刚好接触到,所以就写下来了,初学者望大家见谅哈。

1.既然是Java对象与XML的转换,所以就需要有个Java类来获得对象,本例子主要涉及到BOY类和测试运行的类

2.代码

 2.1 开始第一步的简单学习

      

    @XmlRootElement

     @XmlAccessorType(XmlAccessType.FIELD)//field只是类上的字段,并不是属性

     public class Boy {

            String name = "xu**";

     }

    public class TestXml {

          public static void  main(String args[]){

              try {

                  JAXBContext jaxbContext = JAXBContext.newInstance(Boy.class);

                  Marshaller marshaller = jaxbContext.createMarshaller(); //将对象转换成XML格式

                  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); //将XML转换成对象

                  Boy boy = new Boy();

                  marshaller.marshal(boy,System.out);//以打印输出的形式显示

                  System.out.println();

                  String xmlStr = "<boy><name>许**</name></boy>";

                  Boy towBoy = (Boy)unmarshaller.unmarshal(new StringReader(xmlStr));

                  System.out.println(towBoy.name);

              } catch (JAXBException e) {

                  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.

              }

          }

    }

      打印的结果是:

                <?xml version="1.0" encoding="UTF-8" standalone="yes"?><boy><name>xu**</name></boy>

                许**

    2.2 当boy变成下面这样子

            @XmlRootElement

            @XmlAccessorType(XmlAccessType.PROPERTY)//property是属性

            public class Boy {

                    String name = "xu**";

            }

             打印的结果是:

                    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><boy/>

                    xu**

    2.3 想要有2.1的结果就需要提供name的get方法来获得属性        

        @XmlRootElement

        @XmlAccessorType(XmlAccessType.PROPERTY)

        public class Boy {

            String name ="123";

            public String getName() {

                return name;

            }        

            public void setName(String name) {

                this.name = name;

            }

        }

    2.4 也可以通过xmlelement注解

         @XmlRootElement

            @XmlAccessorType(XmlAccessType.PROPERTY)

            public class Boy {

                String name ="123";

                @XmlElement

                Integer age = 10;

                public String getName() {

                    return name;

                }

                public void setName(String name) {

                    this.name = name;

                }

            }

            打印的结果是:

                <?xml version="1.0" encoding="UTF-8" standalone="yes"?><boy><age>10</age><name>123</name></boy>

                许**

        2.5 同时也可以将BOY改标签,并放在一个命名空间下           

            @XmlRootElement (name = "xu",namespace = "http://test")

            @XmlAccessorType(XmlAccessType.PROPERTY)

            public class Boy {

                @XmlElement

                String name ="123";

                @XmlElement

                Integer age = 10;

            

            }

           public class TestXml {

                  public static void  main(String args[]){

                      try {

                          JAXBContext jaxbContext = JAXBContext.newInstance(Boy.class);

                          Marshaller marshaller = jaxbContext.createMarshaller(); //将对象转换成XML格式

                          Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); //将XML转换成对象

                          Boy boy = new Boy();

                          marshaller.marshal(boy,System.out);

                          System.out.println();

                          String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:xu xmlns:ns2=\"http://test\"><name>许**</name><age>25</age></ns2:xu>";

                          Boy towBoy = (Boy)unmarshaller.unmarshal(new StringReader(xmlStr));

                          System.out.println(towBoy.name);

                          System.out.println(towBoy.age);

                      } catch (JAXBException e) {

                          e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.

                      }

                  }

            }

            打印的结果是:

                <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:xu xmlns:ns2="http://test"><name>123</name><age>10</age></ns2:xu>

                许**

                25

  
            2.6   @XmlJavaTypeAdaptor

            @XmlRootElement (name = "xu",namespace = "http://test")

            @XmlAccessorType(XmlAccessType.PROPERTY)

            public class Boy {

                @XmlElement

                String name ="许**";

                @XmlElement

                Integer age = 25; 

               private TestXmlInterface testXmlInterface;          

            }

              我们要多写一个类TestXmlInterfaceAdaptor用来返回TestXmlInterface 的一个具体实现类的推向

              在转换成XML时接口TestXmlInterface 无法被转换,我们得加上@XmlJavaTypeAdaptor(TestXmlInterfaceAdaptor.class)


你可能感兴趣的:(xml,javaobject)