将xml文档导入jaxb创建的实体结构中 [英] importing xml document into entity structure created by jaxb
问题描述
我正在尝试使用jaxb将xml文档导入由jaxb创建的实体结构中。我下载了 jaxb 2.2.7 并使用了该下载中包含的create-marshal示例,但我更改了内容 po.xsd
到以下内容:
I am trying to use jaxb to import an xml document into an entity structure that was created by jaxb. I downloaded jaxb 2.2.7 and am using the create-marshal sample included in that download, but I changed the content of po.xsd
to the following:
<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
:
I then created po.xml
with the following structure, which is intended to match 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
导入到使用<$ c从 po.xsd
创建的实体结构中$ c> create-marshal 示例 build.xml
文件:
I changed Main.java
to the following form in order to import po.xml
into the entity structure that was created from po.xsd
using the create-marshal
sample build.xml
file:
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的编译部分是:
The compile portion of build.xml is:
<!--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>
但我得到以下堆栈跟踪:
But I am getting the following stack trace:
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
推荐答案
您的问题在这里:
is = new FileInputStream(filename);
is.close();
因此,您尝试使用 FileInputStream $ c从文件解组XML $ c>为此,但关闭
是 unmarshal
之后不是吗?
So, you try to unmarshal XML from file using FileInputStream
for that, but you close is
before unmarshal
not after ?
UPDATE
我从来没有将XSD用于JAXB,只是它在域类上的注释,但你的XSD是错误的。尝试从XSD开头删除 documentTitle
元素。
I have never used XSD for JAXB, just its annotations on domain classes, but your XSD is stil wrong. Try remove documentTitle
element from the beginning of your XSD.
这篇关于将xml文档导入jaxb创建的实体结构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!