[英]JAXB unmarshal XML with element dynamic name
I need read this xml file with JABX when Application began:应用程序开始时,我需要使用 JABX 阅读此 xml 文件:
<local>
<titles>
<main>Aplication</main>
<edit>Change data </edit>
<price>value of </price>
<level>your level </level>
</titles>
<errors>
<generic>Generic Error</generic>
<security>Not Allows</security>
<data>data error </data>
</errors>
</local>
I tried this code:我试过这段代码:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Local {
@XmlAnyElement private Element titles;
@XmlAnyElement private Element errors;
public Element getTitles() { return titles; }
public Element getErrors() { return errors; }
}
but, I have this error message但是,我有这个错误信息
java.lang.IllegalArgumentException: Can not set javax.lang.model.element.Element field swingexample.pojo.Locale.titulos to com.sun.org.apache.xerces.internal.dom.ElementNSImpl at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) at java.lang.reflect.Field.set(Field.java:764) java.lang.IllegalArgumentException: Can not set javax.lang.model.element.Element field swingexample.pojo.Locale.titulos to com.sun.org.apache.xerces.internal.dom.ElementNSImpl at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException (UnsafeFieldAccessorImpl.java:167) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) at java.lang.reflect.Field.set(Field. java:764)
Can anyone help me on this?谁可以帮我这个事?
You can look at that code to unmarshall objects with JAXB.您可以查看该代码以使用 JAXB 解组对象。 I bet you will find everything you need inside it.我打赌你会在里面找到你需要的一切。 Code found on that example of howtodoinjava.com在howtodoinjava.com 示例中找到的代码
Employee.java员工.java
package com.howtodoinjava.demo.model;
import java.io.Serializable;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String firstName;
private String lastName;
private Department department;
public Employee() {
super();
}
//Setters and Getters
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", department="
+ department + "]";
}
// It is called immediately after the object is created and before the unmarshalling begins.
// The callback provides an opportunity to initialize JavaBean properties prior to unmarshalling.
void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
System.out.println("Before Unmarshaller Callback");
}
// It is called after all the properties are unmarshalled for this object,
// but before this object is set to the parent object.
void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
System.out.println("After Unmarshaller Callback");
}
}
JaxbExample.java JaxbExample.java
package com.howtodoinjava.demo;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.howtodoinjava.demo.model.Employee;
public class JaxbExample
{
public static void main(String[] args)
{
String fileName = "employee.xml";
jaxbXmlFileToObject(fileName);
}
private static void jaxbXmlFileToObject(String fileName) {
File xmlFile = new File(fileName);
JAXBContext jaxbContext;
try
{
jaxbContext = JAXBContext.newInstance(Employee.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile);
System.out.println(employee);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
employee.xml员工.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
<department>
<id>101</id>
<name>IT</name>
</department>
<firstName>Lokesh</firstName>
<id>1</id>
<lastName>Gupta</lastName>
</employee>
Thanks by help, but the real question is the element name dynamic, then I can receve a xml like:感谢帮助,但真正的问题是元素名称动态,然后我可以收到 xml 之类的:
<local>
<titles>
<main>Aplication</main>
<edit>Change data </edit>
<price>value of </price>
<level>your level </level>
</titles>
<errors>
<generic>Generic Error</generic>
<security>Not Allows</security>
<data>data error </data>
</errors>
</local>
or like:或喜欢:
<local>
<titles>
<app>Aplication</app>
<splash>Splash message </splash>
</titles>
<errors>
<err>Generic Error</err>
<validate>Not valid</validate>
</errors>
</local>
The titles and errors elements are constants, but inside this elements I can receve any kind of element(main, app, edit, ... etc)标题和错误元素是常量,但在这些元素中我可以接收任何类型的元素(主、应用、编辑等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.