[英]JAXB unmarshal XML with element dynamic name
應用程序開始時,我需要使用 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>
我試過這段代碼:
@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; }
}
但是,我有這個錯誤信息
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)
誰可以幫我這個事?
您可以查看該代碼以使用 JAXB 解組對象。 我打賭你會在里面找到你需要的一切。 在howtodoinjava.com 示例中找到的代碼
員工.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
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();
}
}
}
員工.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>
感謝幫助,但真正的問題是元素名稱動態,然后我可以收到 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>
或喜歡:
<local>
<titles>
<app>Aplication</app>
<splash>Splash message </splash>
</titles>
<errors>
<err>Generic Error</err>
<validate>Not valid</validate>
</errors>
</local>
標題和錯誤元素是常量,但在這些元素中我可以接收任何類型的元素(主、應用、編輯等)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.