Import XML document into entity structure created by jaxb

I am trying to use jaxb to import an XML document into an entity structure created by jaxb. I downloaded jaxb 2.2.7 and use the sample marshal included in this download, but I changed the contents po.xsdto 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>

Then I created po.xmlwith the following structure, which should 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>  

I changed Main.javato the following form to import po.xmlinto an entity structure created from po.xsdusing the create-marshalsample file 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();
    }
}

The compiled part of 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>

But I get 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
0
source share
1

:

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

, XML FileInputStream , is unmarshal ?

UPDATE

XSD JAXB, , XSD . documentTitle XSD.

+2

Source: https://habr.com/ru/post/1527265/


All Articles