JAVA - unMarshaller没有返回相同的对象 - 代码示例

时间:2013-03-19 11:22:26

标签: java jaxb marshalling unmarshalling

我正在为一个简单的java类尝试Marshaller和unMarshaller的测试代码。

我创建了我的对象并设置了它的ID。遇到的问题是在unMarshalling它回到java对象后,ID变量不一样......

我的问题是:

  1. 为什么这不起作用且两个对象都没有相同的ID?
  2. 我的java类必须有一个所有变量的set方法?或者Marshaller是否使用不同的方式来更新类变量?
  3. 这是我的代码段:

    package test;
    
    
    import java.io.StringReader;
    import java.io.StringWriter;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    import javax.xml.bind.annotation.XmlRootElement;
    import org.junit.Test;
    
    public class test {
    
        @Test
        public void marshallingTest() throws JAXBException{
    
            // Create a JAXB context passing in the class of the object we want to marshal/unmarshal
            final JAXBContext context = JAXBContext.newInstance(JavaObject.class);
    
            // Create the marshaller
            final Marshaller marshaller = context.createMarshaller();
    
            // Create a stringWriter to hold the XML
            final StringWriter stringWriter = new StringWriter();
    
            // Create my java object
            final JavaObject javaObject = new JavaObject();
    
            // set the objects ID
            javaObject.setId(90210);
    
            // Marshal the javaObject and write the XML to the stringWriter
            marshaller.marshal(javaObject, stringWriter);
    
            // Print out the contents of the stringWriter
            System.out.println("First object :");
            System.out.println(javaObject.toString());
    
            // Create the unmarshaller
            final Unmarshaller unmarshaller = context.createUnmarshaller();
    
            // Unmarshal the XML 
            final JavaObject javaObject2 = (JavaObject) unmarshaller.unmarshal(new StringReader(stringWriter.toString()));
    
            //print the new object
            System.out.println("Second object after unmarshaller :");
            System.out.println(javaObject2.toString());
        }
    
        @XmlRootElement
        private static class JavaObject {
            private int id;
    
            public JavaObject() {
            }
    
            private int getId() {
                return id;
            }
            private void setId(int id) {
                this.id = id;
            }
            @Override
            public String toString() {
                return "id = "+id;
            }
        }
    }
    

    这是输出:

    First object :
    id = 90210
    Second object after unmarshaller :
    id = 0
    

4 个答案:

答案 0 :(得分:3)

默认情况下,JAXB(JSR-222)实现会将所有公共字段和属性视为已映射。您的访问者(获取/设置)方法应为public,您当前将其设为private。或者您可以在类上指定@XmlAccessorType(XmlAccessType.FIELD),这会告诉JAXB映射可能是私有的字段。

了解更多信息

答案 1 :(得分:0)

试试这个:

@XmlElement    
private void setId(int id) {
        this.id = id;
    }

答案 2 :(得分:0)

  @XmlRootElement 
    private static class JavaObject {
         @XmlElement 
        private int id;

答案 3 :(得分:0)

您需要指定哪些字段是xml结构的一部分。

您可以按照之前的答案建议添加@XmlElement,也可以使用@XmlACcessorType注释:

  @XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  private static class JavaObject {
  }

默认情况下,这将访问您班级的所有字段