Java JAXB-marshaller&;拆封工
我使用JAXB封送器将对象封送到System.out,如下所示:Java JAXB-marshaller&;拆封工,java,jaxb,jaxb2,Java,Jaxb,Jaxb2,我使用JAXB封送器将对象封送到System.out,如下所示: JAXBContext ctxt = JAXBContext.newInstance(CONTEXT); Marshaller m = ctxt.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(map, System.out); Unmarshaller um = MarshalUtils.
JAXBContext ctxt = JAXBContext.newInstance(CONTEXT);
Marshaller m = ctxt.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(map, System.out);
Unmarshaller um = MarshalUtils.getUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File(SOURCE_REGION_SCHEMA));
um.setSchema(schema);
SourceRegionMap srm = (SourceRegionMap) um.unmarshal(input);
然后将输出保存到x.xml文件中,格式为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:sourceCategoryMap xmlns:ns2="http://schemas.xyz.com/services/1.0">
<ns2:source-category-map>
<entry>
<key>Seattle</key>
<map>
<entry>
<key>Restaurant</key>
<value>Restaurant</value>
</entry>
<entry>
<key>Fun</key>
<value>Entertainment</value>
</entry>
</map>
</entry>
<entry>
<key>Honolulu</key>
<map>
<entry>
<key>Food</key>
<value>Restaurant</value>
</entry>
</map>
</entry>
</ns2:source-category-map>
</ns2:sourceCategoryMap>
但它抱怨说:
javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ns2:sourceCategoryMap'.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
有人知道这里出了什么事吗?非常感谢 不要从XML生成XSD。请JAXB使用schemagen命令从java代码生成它。运行schemagen工具。
javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ns2:sourceCategoryMap'.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)