Binding EclipseLink MOXy:绑定使用解组器,但不使用绑定器
我使用相同的绑定配置进行解组,一次使用解组器,一次使用绑定器。第一种方法工作正常,第二种方法抛出异常。原因是什么 输入:Binding EclipseLink MOXy:绑定使用解组器,但不使用绑定器,binding,jaxb,eclipselink,unmarshalling,moxy,Binding,Jaxb,Eclipselink,Unmarshalling,Moxy,我使用相同的绑定配置进行解组,一次使用解组器,一次使用绑定器。第一种方法工作正常,第二种方法抛出异常。原因是什么 输入: <?xml version="1.0" encoding="UTF-8"?> <foo:root xmlns:foo="http://www.domain.org/foo">test</foo:root> 演示: <?xml version="1.0"?> <xml-bindings xmlns="http://www
<?xml version="1.0" encoding="UTF-8"?>
<foo:root xmlns:foo="http://www.domain.org/foo">test</foo:root>
演示:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="test">
<xml-schema element-form-default="QUALIFIED" namespace="http://www.domain.org/foo">
<xml-ns prefix="foo" namespace-uri="http://www.domain.org/foo" />
</xml-schema>
<java-types>
<java-type name="Root">
<xml-root-element name="root"/>
<java-attributes>
<xml-value java-attribute="text"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
package test;
public class Root {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "bindings.xml");
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] { Root.class}, jaxbContextProperties);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Root root = (Root)unmarshaller.unmarshal(new File("input.xml"));
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = documentBuilder.parse(new File("input.xml"));
Binder<Node> binder = jaxbContext.createBinder();
root = (Root) binder.unmarshal(document);
Exception in thread "main" javax.xml.bind.UnmarshalException - with linked exception:
[Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.5.0.v20130507- 3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor with default root element foo:root was not found in the project]
您只需将代码更改为如下所示,以确保您的DocumentBuilderFactory
具有名称空间感知功能即可:
DocumentBuilderFactory DocumentBuilderFactory=DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder DocumentBuilder=documentBuilderFactory.newDocumentBuilder();
documentdocument=documentBuilder.parse(新文件(“input.xml”);