当前位置: 首页 > 知识库问答 >
问题:

将xml文档导入到jaxb创建的实体结构中

法和硕
2023-03-14
<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>
<?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>  
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();
    }
}
<!--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

共有1个答案

段干飞翔
2023-03-14

你的问题在这里:

is = new FileInputStream(filename);
is.close(); 

因此,您尝试使用fileinputstream从文件中反封送XML,但您在unmarshal之前关闭,而不是在unmarshal之后关闭

更新

 类似资料:
  • 我对如何做到这一点有点困惑,所有的文档/示例都展示了如何读取和编辑xml文档,但似乎没有任何从头开始创建xml的明确方法,我宁愿不必将我的程序与虚拟xml文件一起发布以编辑一个。有什么想法吗?谢谢。

  • 例子 (Demo Example) 这是我们需要创建的XML - <?xml version = "1.0" encoding = "UTF-8"?> <cars> <supercars company = "Ferrari"> <carname type = "formula one">Ferrari 101</carname> <carname type = "s

  • XPath parser is used to navigate XML Documents only. It is better to use DOM parser for creating XML. Please refer the Java DOM Parser section for the same.

  • 例子 (Demo Example) 这是我们需要创建的XML - <?xml version = "1.0" encoding = "UTF-8" standalone = "no"?> <cars> <supercars company = "Ferrari"> <carname type = "formula one">Ferrari 101</carname>

  • 例子 (Demo Example) 这是我们需要创建的XML文件 - <?xml version = "1.0" encoding = "UTF-8"?> <cars> <supercars company = "Ferrari"> <carname type = "formula one">Ferrari 101</carname> <carname type =

  • It is better to use StAX parser for creating XML documents rather than using SAX parser. Please refer the Java StAX Parser section for the same.