【问题标题】:XML annotations (JAXB) suggestions for unmarshalling and marshalling用于解组和编组的 XML 注释 (JAXB) 建议
【发布时间】:2021-10-20 23:23:09
【问题描述】:

我有一个示例 XML,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Education>
    <School name="ABC" location="Mangalore">
        <Student gender="M" housepincode="575020">
            <FirstName>VJ</FirstName>
            <LastName>K</LastName>
        </Student>
        <Student gender="M" housepincode="575002">
            <FirstName>S</FirstName>
            <LastName>K</LastName>
        </Student>
    </School>
</Education>

这已在 JAXB 的帮助下使用以下方法解组。 我有针对教育、学校和学生的课程。

教育.java

import lombok.Data;
@Data
@XmlRootElement(name = "Education")
public class Education {    
    public School School;
}

School.java

import lombok.Data;
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class School {   
    @XmlAttribute
    private String name;    
    @XmlAttribute
    private String location;    
    @XmlElement(name ="Student")
    private List<Student> Student;
}

学生.java

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {      
    @XmlAttribute
    private String gender;  
    @XmlAttribute
    private String housepincode;    
    @XmlElement(name = "FirstName")
    private String FirstName;
    @XmlElement(name = "LastName")
    private String LastName;
}

我对收到的输出有些担心。首先有一些警告。我相信这是因为我错过了一些应该存在的注释。请问这里有什么建议或指导吗?

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

Education(School=School(name=ABC, location=Mangalore, Student=[Student(gender=M, housepincode=575020, FirstName=VJ, LastName=K), Student(gender=M, housepincode=575002, FirstName=S, LastName=K)]))

此外,输出的层次结构对我来说似乎不正确,例如:学校 = 学校标签? 我想将此输出打印到单独的 XML 以验证结果。 但是脚本失败了

主程序:

List<Education> Entries = new ArrayList<Education>();
        try {
            File xmlFile = new File("Withattributes.xml");
            JAXBContext jaxbContext;
            jaxbContext = JAXBContext.newInstance(Education.class);                          
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();        
            Education entries = (Education) jaxbUnmarshaller.unmarshal(xmlFile);    
            Entries.add(entries); //storing objects in a list for later use
            
            
            //--------------------------------------------------------------
            JAXBContext jaxbContext_w = JAXBContext.newInstance(Education.class);           
            Marshaller jaxbMarshaller = jaxbContext_w.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            jaxbMarshaller.marshal(jaxbContext_w, System.out);
}
        catch (JAXBException e) 
        {
            e.printStackTrace();
        } catch (FactoryConfigurationError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

我收到以下错误消息:

Neither the class com.sun.xml.bind.v2.runtime.JAXBContextImpl nor one of the associated superclasses is known to this context.
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:593)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:482)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:328)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:256)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:110)
    at JaxbExample.main(JaxbExample.java:39)

请让我知道在这里可以做什么..


更新:

如果 XML 更新为具有如下结构:

<?xml version="1.0" encoding="UTF-8"?>
<Education>
    <School name="ABC" location="Mangalore">
        <Student gender="M" housepincode="575020">
            <FirstName>VJ</FirstName>
            <LastName>K</LastName>
            <Hobbies>
                  <Hobby time = "M">Novels</Hobby>
                  <Hobby time = "A">Gaming</Hobby>
            </Hobbies>
        </Student>
    </School>
</Education>

现在 Student.java 的类将变为

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {      
    @XmlAttribute
    private String gender;  
    @XmlAttribute
    private String housepincode;    
    @XmlElement(name = "FirstName")
    private String FirstName;
    @XmlElement(name = "LastName")
    private String LastName;
    @XmlElementWrapper(name ="Hobbies")
    @XmlElement(name="Hobby")
    private List<Hobby> Hobbies;
}

和 Hobby.java

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Hobby{
    
    @XmlAttribute(name="Time")
    private String Time;
    
    @XmlElement
    private String Hobby;

}

通过这个修改,我没有得到 Hobby 标签的值。我得到的爱好和价值观列表是这样的:

            Hobbies>
                  <Hobby time = "M"/>
                  <Hobby time = "A"/>
            </Hobbies>

你能在这里纠正我吗?

【问题讨论】:

  • 对于更新后的代码,在Private String Hobby 的Hobby 类中使用注释@XmlValue 而不是@XmlElement。我相信这应该有效。如果没有,我会调查的。
  • 您的建议对我有帮助! :)
  • 很高兴知道它有效。也为答案投票。

标签: java xml-parsing jaxb marshalling unmarshalling


【解决方案1】:

第一期: 我不是 JAXB 方面的专家,所以我不知道什么会导致此处指定的警告,但我尝试按照代码进行操作,它似乎对我来说工作正常,没有任何警告。如果您想获得更多了解,那么最好的办法是搜索它并阅读文档以获取更多信息。但我的猜测是,这是由于您的第二个问题而发生的。

另外,我正在使用Moxy,它是JAXB 的扩展,由Eclipse-link 开发。与简单的JAXB相比,它有许多额外的好处

第二期: 明显的错误在这一行:

jaxbMarshaller.marshal(jaxbContext_w, System.out);

你为什么要提供jaxbContext_w 作为它的输入。它应该是未编组的值。在你的情况下,它是entries。

以下是完整代码: 教育:

@Data
@XmlRootElement(name = "Education")
@XmlAccessorType(XmlAccessType.FIELD)
public class Education {
    public School School;
}

学校:

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class School {
    @XmlAttribute
    private String name;

    @XmlAttribute
    private String location;

    @XmlElement
    private List<Student> Student;
}

学生:

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {
    @XmlAttribute
    private String gender;

    @XmlAttribute
    private String housepincode;

    @XmlElement
    private String FirstName;

    @XmlElement
    private String LastName;
}

主要:


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(Education.class).createUnmarshaller();
        final Education education = unmarshaller.unmarshal(xmlStreamReader, Education.class).getValue();
        System.out.println(education.toString());

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

输出(没有任何错误或警告):


Education(School=School(name=ABC, location=Mangalore, Student=[Student(gender=M, housepincode=575020, FirstName=VJ, LastName=K), Student(gender=M, housepincode=575002, FirstName=S, LastName=K)]))
<Education>
   <School name="ABC" location="Mangalore">
      <Student gender="M" housepincode="575020">
         <FirstName>VJ</FirstName>
         <LastName>K</LastName>
      </Student>
      <Student gender="M" housepincode="575002">
         <FirstName>S</FirstName>
         <LastName>K</LastName>
      </Student>
   </School>
</Education>

【讨论】:

  • 对警告来说这很奇怪.. 是的,我认为第二个问题的错误.. 谢谢你的帮助,我也需要有关更新的指导.. 你能调查一下吗跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 2019-11-25
  • 2011-12-20
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
相关资源
最近更新 更多