Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA-unMarshaller不返回相同的对象代码示例_Java_Jaxb_Marshalling_Unmarshalling - Fatal编程技术网

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

JAVA-unMarshaller不返回相同的对象代码示例,java,jaxb,marshalling,unmarshalling,Java,Jaxb,Marshalling,Unmarshalling,我正在一个简单的java类上尝试Marshaller和unMarshaller的测试代码 我创建了我的对象并设置了它的ID。遇到的问题是在将它解组回java对象后,ID变量不一样 我的问题是: 为什么这不起作用,而且两个对象没有相同的ID 我的java类必须为所有变量提供一个set方法?还是Marshaller使用不同的方法来更新类变量 这是我的代码片段: package test; import java.io.StringReader; import java.io.StringWrit

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

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

我的问题是:

  • 为什么这不起作用,而且两个对象没有相同的ID
  • 我的java类必须为所有变量提供一个set方法?还是Marshaller使用不同的方法来更新类变量
  • 这是我的代码片段:

    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
    
    试试这个:

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

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

    您可以按照前面的答案添加
    @xmlement
    ,也可以使用
    @xmlacessortype
    注释:

      @XmlRootElement
      @XmlAccessorType(XmlAccessType.FIELD)
      private static class JavaObject {
      }
    
    默认情况下,它将访问类的所有字段。默认情况下,JAXB(JSR-222)实现将把所有公共字段和属性都当作映射处理。您的访问器(get/set)方法应该是
    public
    ,当前它们是
    private
    。或者您可以在类上指定
    @xmlacessortype(xmlacesstype.FIELD)
    ,这会告诉JAXB映射可能是私有的字段

    了解更多信息

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