将XML文件转换为具有List的XML对象 [英] Convert XML file into an XML object having a List
问题描述
我有这样的XML。我想将其转换为JAVA对象。
I have a XML like this . And i want to convert it into JAVA object.
<P1>
<CTS>
Hello
</CTS>
<CTS>
World
</CTS>
<P1>
所以我创建了以下带有属性的java类。
So I created following java classes with their properties.
P1类
@XmlRootElement
public class P1 {
@XmlElement(name = "CTS")
List<CTS> cts;
}
CTS class
public class CTS {
String ct;
}
测试类
File file = new File("D:\\ContentTemp.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(P1.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
P1 p = (P1) jaxbUnmarshaller.unmarshal(file);
但我收到以下错误 -
But I am getting following error -
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:
2 counts of IllegalAnnotationExceptions
Class has two properties of the same name "cts"
推荐答案
更新
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:2
IllegalAnnotationExceptions类的数量有两个属性
同名cts
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions Class has two properties of the same name "cts"
默认情况下a JAXB(JSR-222) 实现基于属性创建映射和注释的字段。当您注释一个也有属性的字段时,它将导致此错误。
By default a JAXB (JSR-222) implementation creates mappings based on properties and annotated fields. When you annotate a field for which there is also a property it will cause this error.
选项#1 - 使用@XmlAccessorType(XmlAccessType.FIELD)
您可以在类上注释需要指定 @XmlAccessorType(XmlAccessType.FIELD)
的字段。
You could annotate the field you need to specify @XmlAccessorType(XmlAccessType.FIELD)
on the class.
@XmlRootElement(name="P1)
@XmlAccessorType(XmlAccessType.FIELD)
public class P1 {
@XmlElement(name = "CTS")
List<CTS> cts;
}
选项#2 - 注释属性(获取方法)
或者你可以注释 get
方法。
@XmlRootElement(name="P1)
public class P1 {
List<CTS> cts;
@XmlElement(name = "CTS")
public List<CTS> getCts() {
return cts;
}
}
更多信息
- http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
完整示例
CTS
您可以使用 @XmlValue
批注将Java类映射到具有简单内容的复杂类型。
You can use the @XmlValue
annotation to map to Java class to a complex type with simple content.
@XmlAccessorType(XmlAccessType.FIELD)
public class CTS {
@XmlValue
String ct;
}
P1
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="P1")
@XmlAccessorType(XmlAccessType.FIELD)
public class P1 {
@XmlElement(name = "CTS")
List<CTS> cts;
}
演示
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(P1.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum13987708/input.xml");
P1 p1 = (P1) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(p1, System.out);
}
}
input.xml /输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<P1>
<CTS>
Hello
</CTS>
<CTS>
World
</CTS>
</P1>
更多信息
- http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
这篇关于将XML文件转换为具有List的XML对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!