我正在使用 jaxb moxy 从活页夹中解组 xml,但它给出了异常:在项目中找不到具有默认根元素 bean 的描述符。我还使用 package-info.java 来指定命名空间。
要解组的 XML 文件-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.example.org/package">
</beans>
Beans.java-
@XmlRootElement(namespace="http://www.example.org/package")
public class Beans {
String name = "ss";
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
包信息.java
@XmlSchema(
namespace="http://www.example.org/package",
elementFormDefault=XmlNsForm.QUALIFIED)
package com.jaxb.test;
import javax.xml.bind.annotation.*;
主班——
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File xml = new File(
"D:\\eclipse-jee-indigo-SR2\beans.xml");
Document document = db.parse(xml);
JAXBContext jc = JAXBContext.newInstance(Beans.class);
Binder<Node> binder = jc.createBinder();
Beans customer = (Beans) jc.createBinder().unmarshal(document);//throws exception
//Beans customer = (Beans) jc.createUnmarshaller().unmarshal(xml);This works
//Beans customer = (Beans) jc.createUnmarshaller().unmarshal(document);Throws same exception
例外-
javax.xml.bind.UnmarshalException
- with linked exception:
[Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.4.1.v20121003- ad44345): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor with default root element beans was not found in the project]
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:1014)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:199)
at com.jaxb.test.JaxbTest.main(JaxbTest.java:43)