覆盖 JAXB 中的对象

Overwriting of objects in JAXB

我有一个示例 XML,想要创建对象并将它们存储在列表中。 XML 包含以下内容:

<students>
<student>
    <firstName>A</firstName>
    <id>1</id>
    <lastName>C</lastName>
    <company>BCD</company>
</student>
<student>
    <firstName>B</firstName>
    <id>2</id>
    <lastName>C</lastName>
    <company>EFG</company>
</student>
</students>

我创建了 类 student.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import lombok.Data;

@Data
@XmlRootElement
public class Student {

    private long Id;
    private String firstName;
    private String lastName;
    private String company;
}

以及Students.java

import javax.xml.bind.annotation.XmlRootElement;

import lombok.Data;

@Data
@XmlRootElement
public class Students {
    
    private Student student;

}

当然还有主程序:ParsingXML.java

List<Students> listOfStudentsType= new ArrayList<Students>();   
        
        
        try {
            File xmlFile = new File("Student.xml");
            JAXBContext jaxbContext;
            jaxbContext = JAXBContext.newInstance(Students.class);                           
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();        
            Students employee = (Students) jaxbUnmarshaller.unmarshal(xmlFile); 
            listOfStudentsType.add(employee);
           
        }
        catch (JAXBException e) 
        {
            e.printStackTrace();
        }
        ListIterator<Students> litr = listOfStudentsType.listIterator();   
        //System.out.println(listOfStudentsType.size());
        //System.out.println("\n Using list iterator");
        while(litr.hasNext()){
            System.out.println(litr.next());
        }

我想将对象存储在内部列表中。 我希望它有 2 个 ID 为 1 和 2 的条目。

但是我得到以下输出

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/C:/Users/R.Premsagar/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.0-b170127.1453/jaxb-runtime-2.3.0-b170127.1453.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

Students(student=Student(Id=2, firstName=B, lastName=C, company=EFG))

此处仅显示第二个条目。我相信它正在被覆盖。任何人都可以提供一些见解让我拥有这两个价值观,请


显而易见的原因是您没有使用 List<Student> 到达 XML。以下是 unmarshallingmarshalling 提供的 XML:

的完整解决方案

XML:

<students>
    <student>
        <firstName>A</firstName>
        <id>1</id>
        <lastName>C</lastName>
        <company>BCD</company>
    </student>
    <student>
        <firstName>B</firstName>
        <id>2</id>
        <lastName>C</lastName>
        <company>EFG</company>
    </student>
</students>

Students.class:

@XmlRootElement(name = "students")
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Students {
    @XmlElement(name="student")
    private List<Student> student;
}

Student.class:

@Data
@XmlAccessorType(XmlAccessType.NONE)
public class Student {
    @XmlElement(name="id")
    private long Id;

    @XmlElement(name="firstName")
    private String firstName;

    @XmlElement(name="lastName")
    private String lastName;

    @XmlElement(name="company")
    private String company;
}

Main.class:

public class Main {
    public static void main(String[] args) throws JAXBException, XMLStreamException {
        final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("students.xml");
        final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        final Unmarshaller unmarshaller = JAXBContext.newInstance(Students.class).createUnmarshaller();
        final Students students = unmarshaller.unmarshal(xmlStreamReader, Students.class).getValue();
        System.out.println(students.toString());

        Marshaller marshaller = JAXBContext.newInstance(Students.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(students, System.out);
    }
}

这将提供以下结果:

Students(student=[Student(Id=1, firstName=A, lastName=C, company=BCD), Student(Id=2, firstName=B, lastName=C, company=EFG)])
<students>
   <student>
      <id>1</id>
      <firstName>A</firstName>
      <lastName>C</lastName>
      <company>BCD</company>
   </student>
   <student>
      <id>2</id>
      <firstName>B</firstName>
      <lastName>C</lastName>
      <company>EFG</company>
   </student>
</students>