【问题标题】:Overwriting of objects in JAXB在 JAXB 中覆盖对象
【发布时间】:2021-10-19 23:49:00
【问题描述】:

我有一个示例 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>

我创建类 学生.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))

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


【问题讨论】:

  • 我不知道这项技术,但在逻辑上不应该是private List&lt;Student&gt; students; in Student
  • 我已经尝试过了,但是给出了一个意外的元素异常
  • 直到有人过来知道这一点,THIS 可能会有所帮助

标签: java xml-parsing jaxb lombok


【解决方案1】:

明显的原因是您没有使用List&lt;Student&gt; 来访问 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>

学生班级:

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

学生班级:

@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;
}

主类:

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>

【讨论】:

  • 感谢您的回复。但是必须使用 InputStream 和 XMLStreamReader 吗?关于我们是否可以在解组后使用简单的 for 循环看到结果的任何建议?最终,我想将学生对象添加到列表中
  • 娜娜,你可以使用任何你想要的东西。就我而言,我正在读取文件,所以我使用了Inputstreamreader。在此链接中,他们根据您的选择提供了您可以为Jaxb marshalling/unmarshalling 使用的输入类型,您可以使用任何东西。只要您的 JAXB 正在获取数据,您使用什么都没关系:docs.oracle.com/javase/7/docs/api/javax/xml/bind/… 和示例:tabnine.com/code/java/methods/javax.xml.bind.JAXBContext/…
猜你喜欢
  • 1970-01-01
  • 2014-09-21
  • 2011-08-19
  • 2019-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多