【发布时间】: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