美好时光!
我有一个xml:
<fieldSet name="Test" desc="Test">
<field name="id">
<fieldinfo>
<name>id</name>
<type>String</type>
<fieldsize>50</fieldsize>
</fieldinfo>
<validators />
</field>
</fieldSet>
我需要使用 JAXB 解析它。我试过这个:
try {
JAXBContext jaxbContext = JAXBContext.newInstance(CustomFieldSet.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xml);
CustomFieldSet fieldSet = (CustomFieldSet) unmarshaller.unmarshal(reader);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
CustomFieldSet 类的开头是:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "fieldSet")
public class CustomFieldSet {
@XmlAttribute(name = "name", required = true)
private String tableName;
@XmlAttribute(name = "desc", required = true)
private String tableDescription;
...
当调用 unmarshal() 函数时,会抛出以下异常:
javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException: Content is not allowed in prolog.]
我认为问题与我的 xml 不包含 xml 声明 ( <?xml ...
) 的事实有关。
有人知道这里的解决方法是什么吗?