通过XSD自动生成JAVA类或者JAR,然后用这个生成 XML 文件

时间:2022-10-21 11:52:07
想问一下,我手上有个 XSD 文件,我想通过这个 XSD 文件,生成一个 Jar 包或者 一些*.class文件,然后用过这个 这个Jar或者 class 来生成一个 XML 文件,或者字符串,请问有这样的工具包么?

3 个解决方案

#1


JDK 自带的 xjc.exe 就可以将XSD转换为Java源代码,然后编译就可以了。
address.xsd
<schema targetNamespace="http://www.example.com/IPO"
        xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:ipo="http://www.example.com/IPO">

 <annotation>
   <documentation xml:lang="en">
     Addresses for International Purchase order schema
     Copyright 2000 Example.com. All rights reserved.
   </documentation>
 </annotation>

 <complexType name="Address">
   <sequence>
     <element name="name" type="string"/>
     <element name="street" type="string"/>
     <element name="city" type="string"/>
   </sequence>
 </complexType>

 <complexType name="USAddress">
   <complexContent>
     <extension base="ipo:Address">
       <sequence>
         <element name="state" type="ipo:USState"/>
         <element name="zip" type="positiveInteger"/>
       </sequence>
     </extension>
   </complexContent>
 </complexType>

 <complexType name="UKAddress">
   <complexContent>
     <extension base="ipo:Address">
       <sequence>
         <element name="postcode" type="ipo:UKPostcode"/>
       </sequence>
       <attribute name="exportCode" type="positiveInteger" fixed="1"/>
     </extension>
   </complexContent>
 </complexType>

 <!-- other Address derivations for more countries -->

 <simpleType name="USState">
   <restriction base="string">
     <enumeration value="AK"/>
     <enumeration value="AL"/>
     <enumeration value="AR"/>
     <!-- and so on ... -->
     <enumeration value="PA"/>
   </restriction>
 </simpleType>

 <!-- simple type definition for UKPostcode -->

 <simpleType name="UKPostcode">
    <restriction base="string">
        <pattern value="[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}"/>
    </restriction>
 </simpleType>
</schema>


xjc address.xsd
parsing a schema...
compiling a schema...
com\example\ipo\Address.java
com\example\ipo\ObjectFactory.java
com\example\ipo\UKAddress.java
com\example\ipo\USAddress.java
com\example\ipo\USState.java
com\example\ipo\package-info.java


mkdir classes
javac -d classes com\example\ipo\*.java

tree.exe classes
classes
`-- com
    `-- example
        `-- ipo
            |-- Address.class
            |-- ObjectFactory.class
            |-- UKAddress.class
            |-- USAddress.class
            |-- USState.class
            `-- package-info.class

#2


https://jaxb.dev.java.net/ 刚才上网查了一下,找到了这个软件,但是好像不太会用,我在cmd下面怎么才能指定 将 C:\ AA.xsd 文件生成一个 *.jar 包呢, 文件的包 叫做 MASSimulator.Mining, 比如下面的xsd中有个元素为 Process ,我想用的时候就写成

Process process = new Process()
ProcessInstance pi = new ProcessInstace();
process.append(pi) //就这样方面的添加节点




 

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XML Spy v4.1 U (http://www.xmlspy.com) by Boudewijn van Dongen (Technische Universiteit Eindhoven) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:simpleType name="eventtypes">
    <xs:restriction base="xs:normalizedString">
      <xs:enumeration value="schedule"/>
      <xs:enumeration value="assign"/>
      <xs:enumeration value="withdraw"/>
      <xs:enumeration value="reassign"/>
      <xs:enumeration value="start"/>
      <xs:enumeration value="suspend"/>
      <xs:enumeration value="resume"/>
      <xs:enumeration value="pi_abort"/>
      <xs:enumeration value="ate_abort"/>
      <xs:enumeration value="complete"/>
      <xs:enumeration value="autoskip"/>
      <xs:enumeration value="manualskip"/>
      <xs:enumeration value="unknown"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="WorkflowLog">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Data" minOccurs="0"/>
        <xs:element ref="Source" minOccurs="0"/>
        <xs:element ref="Process" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="description" type="xs:normalizedString" use="optional"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="Source">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Data" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute name="program" type="xs:normalizedString" use="required"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="Process">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Data" minOccurs="0"/>
        <xs:element ref="ProcessInstance" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:normalizedString" use="required"/>
      <xs:attribute name="description" type="xs:normalizedString" use="optional"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="AuditTrailEntry">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Data" minOccurs="0"/>
        <xs:element name="WorkflowModelElement" type="xs:normalizedString"/>
        <xs:element name="EventType">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="eventtypes">
                <xs:attribute name="unknowntype" type="xs:normalizedString" use="optional"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:element name="Timestamp" minOccurs="0">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:dateTime"/>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:element name="Originator" type="xs:normalizedString" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Data">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Attribute" maxOccurs="unbounded">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:normalizedString">
                <xs:attribute name="name" type="xs:normalizedString" use="required"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="ProcessInstance">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Data" minOccurs="0"/>
        <xs:element ref="AuditTrailEntry" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:normalizedString" use="required"/>
      <xs:attribute name="description" type="xs:normalizedString" use="optional"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

#3


http://www.google.com/search?&q=jaxb+xsd+xjc+site%3Ajavaeye.com

http://www.javaeye.com/wiki/topic/376740

#1


JDK 自带的 xjc.exe 就可以将XSD转换为Java源代码,然后编译就可以了。
address.xsd
<schema targetNamespace="http://www.example.com/IPO"
        xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:ipo="http://www.example.com/IPO">

 <annotation>
   <documentation xml:lang="en">
     Addresses for International Purchase order schema
     Copyright 2000 Example.com. All rights reserved.
   </documentation>
 </annotation>

 <complexType name="Address">
   <sequence>
     <element name="name" type="string"/>
     <element name="street" type="string"/>
     <element name="city" type="string"/>
   </sequence>
 </complexType>

 <complexType name="USAddress">
   <complexContent>
     <extension base="ipo:Address">
       <sequence>
         <element name="state" type="ipo:USState"/>
         <element name="zip" type="positiveInteger"/>
       </sequence>
     </extension>
   </complexContent>
 </complexType>

 <complexType name="UKAddress">
   <complexContent>
     <extension base="ipo:Address">
       <sequence>
         <element name="postcode" type="ipo:UKPostcode"/>
       </sequence>
       <attribute name="exportCode" type="positiveInteger" fixed="1"/>
     </extension>
   </complexContent>
 </complexType>

 <!-- other Address derivations for more countries -->

 <simpleType name="USState">
   <restriction base="string">
     <enumeration value="AK"/>
     <enumeration value="AL"/>
     <enumeration value="AR"/>
     <!-- and so on ... -->
     <enumeration value="PA"/>
   </restriction>
 </simpleType>

 <!-- simple type definition for UKPostcode -->

 <simpleType name="UKPostcode">
    <restriction base="string">
        <pattern value="[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}"/>
    </restriction>
 </simpleType>
</schema>


xjc address.xsd
parsing a schema...
compiling a schema...
com\example\ipo\Address.java
com\example\ipo\ObjectFactory.java
com\example\ipo\UKAddress.java
com\example\ipo\USAddress.java
com\example\ipo\USState.java
com\example\ipo\package-info.java


mkdir classes
javac -d classes com\example\ipo\*.java

tree.exe classes
classes
`-- com
    `-- example
        `-- ipo
            |-- Address.class
            |-- ObjectFactory.class
            |-- UKAddress.class
            |-- USAddress.class
            |-- USState.class
            `-- package-info.class

#2


https://jaxb.dev.java.net/ 刚才上网查了一下,找到了这个软件,但是好像不太会用,我在cmd下面怎么才能指定 将 C:\ AA.xsd 文件生成一个 *.jar 包呢, 文件的包 叫做 MASSimulator.Mining, 比如下面的xsd中有个元素为 Process ,我想用的时候就写成

Process process = new Process()
ProcessInstance pi = new ProcessInstace();
process.append(pi) //就这样方面的添加节点




 

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XML Spy v4.1 U (http://www.xmlspy.com) by Boudewijn van Dongen (Technische Universiteit Eindhoven) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:simpleType name="eventtypes">
    <xs:restriction base="xs:normalizedString">
      <xs:enumeration value="schedule"/>
      <xs:enumeration value="assign"/>
      <xs:enumeration value="withdraw"/>
      <xs:enumeration value="reassign"/>
      <xs:enumeration value="start"/>
      <xs:enumeration value="suspend"/>
      <xs:enumeration value="resume"/>
      <xs:enumeration value="pi_abort"/>
      <xs:enumeration value="ate_abort"/>
      <xs:enumeration value="complete"/>
      <xs:enumeration value="autoskip"/>
      <xs:enumeration value="manualskip"/>
      <xs:enumeration value="unknown"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="WorkflowLog">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Data" minOccurs="0"/>
        <xs:element ref="Source" minOccurs="0"/>
        <xs:element ref="Process" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="description" type="xs:normalizedString" use="optional"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="Source">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Data" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute name="program" type="xs:normalizedString" use="required"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="Process">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Data" minOccurs="0"/>
        <xs:element ref="ProcessInstance" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:normalizedString" use="required"/>
      <xs:attribute name="description" type="xs:normalizedString" use="optional"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="AuditTrailEntry">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Data" minOccurs="0"/>
        <xs:element name="WorkflowModelElement" type="xs:normalizedString"/>
        <xs:element name="EventType">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="eventtypes">
                <xs:attribute name="unknowntype" type="xs:normalizedString" use="optional"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:element name="Timestamp" minOccurs="0">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:dateTime"/>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:element name="Originator" type="xs:normalizedString" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Data">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Attribute" maxOccurs="unbounded">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:normalizedString">
                <xs:attribute name="name" type="xs:normalizedString" use="required"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="ProcessInstance">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Data" minOccurs="0"/>
        <xs:element ref="AuditTrailEntry" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:normalizedString" use="required"/>
      <xs:attribute name="description" type="xs:normalizedString" use="optional"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

#3


http://www.google.com/search?&q=jaxb+xsd+xjc+site%3Ajavaeye.com

http://www.javaeye.com/wiki/topic/376740