我正在尝试使用jaxb将xml文档导入由jaxb创建的实体结构中。我下载了jaxb 2.2.7并使用了该下载中包含的create-marshal示例,但我将po.xsd
的内容更改为以下内容:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="businessDocument" type="BusinessDocument"/>
<xsd:complexType name="CompoundId">
<xsd:attribute name="root" type="xsd:string"/>
<xsd:attribute name="extension" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="EntityType1">
<xsd:sequence>
<xsd:element name="typeId" type="CompoundId"/>
<xsd:element name="entityTitle" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BusinessDocument">
<xsd:sequence>
<xsd:element name="typeId" type="CompoundId"/>
<xsd:element ref="documentTitle" minOccurs="1"/>
<xsd:element name="entity1" type="EntityType1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
然后我使用以下结构创建po.xml
,该结构旨在匹配po.xsd
:
<?xml version="1.0"?>
<businessDocument>
<typeId root="docroot1" extension="docext1"/>
<documentTitle>first document</documentTitle>
<entity1>
<typeId root="innerroot1" extension="innerext1"/>
<entityTitle>inner title</entityTitle>
</entity1>
</businessDocument>
我将Main.java
更改为以下表单,以便将po.xml
导入使用po.xsd
示例create-marshal
文件从build.xml
创建的实体结构中:
package mainpackage;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import primer.po.*;
import java.io.*;
public class Main {
public static void main( String[] args ) {
String filename = "C:\\Temp\\jaxb\\apps\\create-marshal\\po.xml";
InputStream is = getInputStream(filename);
try { unmarshal(BusinessDocument.class, is);}
catch (JAXBException e) {e.printStackTrace();}
}
public static InputStream getInputStream(String filename){
InputStream is = null;
try {
is = new FileInputStream(filename);
is.close();
}
catch (FileNotFoundException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
return is;
}
public static <T> T unmarshal( Class<T> docClass, InputStream inputStream )throws JAXBException {
String packageName = docClass.getPackage().getName();
JAXBContext jc = JAXBContext.newInstance( packageName );
Unmarshaller u = jc.createUnmarshaller();
JAXBElement<T> doc = (JAXBElement<T>)u.unmarshal( inputStream );
return doc.getValue();
}
}
build.xml的编译部分是:
<!--compile Java source files-->
<target name="compile" description="Compile all Java source files">
<echo message="Compiling the schema..." />
<mkdir dir="gen-src" />
<mkdir dir="gen-src/primer/po" />
<xjc schema="po.xsd" package="primer.po" destdir="gen-src">
<produces dir="gen-src/primer/po" includes="**/*.java" />
</xjc>
<echo message="Compiling the java source files..." />
<mkdir dir="classes" />
<javac destdir="classes" debug="on">
<src path="src" />
<src path="gen-src" />
<classpath refid="classpath" />
</javac>
</target>
但我得到以下堆栈跟踪:
javax.xml.bind.UnmarshalException
- with linked exception:
[java.io.IOException: Read error]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
at mainpackage.Main.unmarshal(Main.java:39)
at mainpackage.Main.main(Main.java:17)
Caused by: java.io.IOException: Read error
at java.io.FileInputStream.read(Native Method)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(XMLEntityManager.java:2932)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:704)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
... 5 more
答案 0 :(得分:2)
您的问题在这里:
is = new FileInputStream(filename);
is.close();
因此,您尝试使用FileInputStream
从文件解组XML,但是在<{1}}之后关闭 is
而不是之后?
<强>更新强>
我从来没有将XSD用于JAXB,只是它在域类上的注释,但是你的XSD是错误的。尝试从XSD开头删除unmarshal
元素。