针对通过 JAXB 生成的 xsd 文件和 Eclipse 中的 XSD 构建选项验证 xml

Validating xml against xsd file generated through JAXB and XSD build option in eclipse

我正在尝试获得一个示例代码,用于根据 xsd 文件验证我的 xml 文件,该文件是通过 jaxb 生成的,xsd 通过 Eclipse 构建。

我正在尝试制作一个样本验证器文件,它根据我的 xsd 文件验证我的 xml。现在,我已经找到了很多 java 代码的例子,我正在使用其中的一个。我还收到了很多关于我面临的问题的答案,但答案以某种方式建议更新我的 xsd 或 xml 文件。对我来说,我不能做任何这些,因为我通过 jaxb 生成 xsd 文件,通过 Eclipse 生成 xsd 构建选项。

try {
SchemaFactory factory = 
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File(xsdPath));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xmlPath)));
} catch (IOException e) {
System.out.println("Exception: "+e.getMessage());
return false;
} catch( SAXException e){
System.out.println("Exception: "+e.getMessage());
return false;
}
return true;
}

My XML File
<!DOCTYPE MLIspec SYSTEM "MLIspec-1.6.dtd"> 
<MLIspec> 
<id>4050response</id> 
<typename>4050Response</typename> 
<description>Show the Contract Numbers</description>    
<element> 
<id>7504</id> 
<name>SourceAccountIdentifier</name> 
<type>table</type> 
<required>true</required>       
</element>
</MLIspec>
XSD file

<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cdcla="cdcla" jxb:version="1.0">
<xs:include schemaLocation="SourceAccountIdentifier.xsd"/>
<xs:complexType name="MXMLI4050Response">
<xs:annotation>
<xs:appinfo>
<jxb:class name="MXMLI4050Response">
<jxb:javadoc>4050response - MXMLI4050Response. Show the Contract Numbers. </jxb:javadoc>
</jxb:class>
<cdcla:elf group="true"/>
</xs:appinfo>
</xs:annotation>
<xs:all>
<xs:element name="e7920" type="xs:integer" minOccurs="0">
<xs:annotation>
<xs:documentation>MessageFunctionCode</xs:documentation>
<xs:appinfo>
<jxb:property name="MessageFunctionCode">
<jxb:javadoc>7920 - MessageFunctionCode. </jxb:javadoc>
<jxb:baseType>
<jxb:javaType name="java.lang.Integer" parseMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.parseIntegerFromDatatypeN" printMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.printIntegerAsDatatypeN"/>
</jxb:baseType>
</jxb:property>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="e8002" type="xs:integer" minOccurs="0">
<xs:annotation>
<xs:documentation>TerminalID</xs:documentation>
<xs:appinfo>
<jxb:property name="TerminalID">
<jxb:javadoc>8002 - TerminalID. </jxb:javadoc>
<jxb:baseType>
<jxb:javaType name="java.lang.Integer" parseMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.parseIntegerFromDatatypeN" printMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.printIntegerAsDatatypeN"/>
</jxb:baseType>
</jxb:property>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="e8005" type="xs:integer" minOccurs="0">
<xs:annotation>
<xs:documentation>MessageSequenceNumber</xs:documentation>
<xs:appinfo>
<jxb:property name="MessageSequenceNumber">
<jxb:javadoc>8005 - MessageSequenceNumber. </jxb:javadoc>
<jxb:baseType>
<jxb:javaType name="java.lang.Integer" parseMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.parseIntegerFromDatatypeN" printMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.printIntegerAsDatatypeN"/>
</jxb:baseType>
</jxb:property>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="e7300" type="xs:integer" minOccurs="0">
<xs:annotation>
<xs:documentation>AcquirerResponseCode</xs:documentation>
<xs:appinfo>
<jxb:property name="AcquirerResponseCode">
<jxb:javadoc>7300 - AcquirerResponseCode. </jxb:javadoc>
<jxb:baseType>
<jxb:javaType name="java.lang.Integer" parseMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.parseIntegerFromDatatypeN" printMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.printIntegerAsDatatypeN"/>
</jxb:baseType>
</jxb:property>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="e7302" type="xs:integer" minOccurs="0">
<xs:annotation>
<xs:documentation>ActionCode</xs:documentation>
<xs:appinfo>
<jxb:property name="ActionCode">
<jxb:javadoc>7302 - ActionCode. </jxb:javadoc>
<jxb:baseType>
<jxb:javaType name="java.lang.Integer" parseMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.parseIntegerFromDatatypeN" printMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.printIntegerAsDatatypeN"/>
</jxb:baseType>
</jxb:property>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="e40986" type="xs:string" minOccurs="0">
<xs:annotation>
<xs:documentation>ActionCodeDescription</xs:documentation>
<xs:appinfo>
<jxb:property name="ActionCodeDescription">
<jxb:javadoc>40986 - ActionCodeDescription. </jxb:javadoc>
</jxb:property>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="e7928" type="xs:string" minOccurs="0">
<xs:annotation>
<xs:documentation>HostID</xs:documentation>
<xs:appinfo>
<jxb:property name="HostID">
<jxb:javadoc>7928 - HostID. </jxb:javadoc>
</jxb:property>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="e8001" type="xs:integer" minOccurs="0">
<xs:annotation>
<xs:documentation>RetryFlag</xs:documentation>
<xs:appinfo>
<jxb:property name="RetryFlag">
<jxb:javadoc>8001 - RetryFlag. </jxb:javadoc>
<jxb:baseType>
<jxb:javaType name="java.lang.Integer" parseMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.parseIntegerFromDatatypeN" printMethod="com.citigroup.cdcla.gmli.bind.DatatypeConverter.printIntegerAsDatatypeN"/>
</jxb:baseType>
</jxb:property>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="e47602" type="xs:integer" minOccurs="0">
<xs:annotation>
<xs:documentation>CustomerTypeCode</xs:documentation>
<xs:appinfo>
<jxb:property name="CustomerTypeCode">
<jxb:javadoc>47602 - CustomerTypeCode. </jxb:javadoc>
</jxb:property>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="e7504" type="SourceAccountIdentifier" maxOccurs="unbounded" minOccurs="0">
<xs:annotation>
<xs:documentation>SourceAccountIdentifierList</xs:documentation>
<xs:appinfo>
<jxb:property name="SourceAccountIdentifierList">
<jxb:javadoc>7504 - SourceAccountIdentifier Table. </jxb:javadoc>
</jxb:property>
<cdcla:elf group="true"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:all>
</xs:complexType>
</xs:schema>

以下是我收到的错误 异常:src-resolve:无法将名称 'SourceAccountIdentifier' 解析为 (n) 'type definition' 组件。

此外,我尝试从我的 xsd 定义中删除 SourceAccountIdentifier,看看它是否在其他地方失败,在这种情况下,我收到以下错误

Exception: src\main\java\com\citi\cbol\test\MLIspec-1.6.dtd (The system cannot find the file specified)

请帮忙提供一些错误的解决方案。谢谢!!

生成的代码在 github 上为您服务。

猜测 这是 xmlxsd 文件的问题 -- 但我不知道事实。

我暂时删除了 dtd,这是我目前拥有的:

thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ ls
foo.xml
thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ cat foo.xml 
<MLIspec> 
<id>4050response</id> 
<typename>4050Response</typename> 
<description>Show the Contract Numbers</description>    
<element> 
<id>7504</id> 
<name>SourceAccountIdentifier</name> 
<type>table</type> 
<required>true</required>       
</element>
</MLIspec>
thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ trang foo.xml foo.xsd
thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ cat foo.xsd 
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="MLIspec">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="id"/>
        <xs:element ref="typename"/>
        <xs:element ref="description"/>
        <xs:element ref="element"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="typename" type="xs:NMTOKEN"/>
  <xs:element name="description" type="xs:string"/>
  <xs:element name="element">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="id"/>
        <xs:element ref="name"/>
        <xs:element ref="type"/>
        <xs:element ref="required"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="name" type="xs:NCName"/>
  <xs:element name="type" type="xs:NCName"/>
  <xs:element name="required" type="xs:boolean"/>
  <xs:element name="id" type="xs:NMTOKEN"/>
</xs:schema>
thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ xjc foo.xsd 
parsing a schema...
compiling a schema...
generated/Element.java
generated/MLIspec.java
generated/ObjectFactory.java
thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ tree
.
├── foo.xml
├── foo.xsd
└── generated
    ├── Element.java
    ├── MLIspec.java
    └── ObjectFactory.java

1 directory, 5 files
thufir@dur:~/xml/so$ 

这些工具是 trangxjc,它们可以很容易地安装在 Ubuntu Linux 上。 下面是如何 运行 从控制台构建的项目:

thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ cat /home/thufir/xml/output.xml 
cat: /home/thufir/xml/output.xml: No such file or directory
thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ git clone git@github.com:THUFIR/xjc_generated-code.git
Cloning into 'xjc_generated-code'...
remote: Enumerating objects: 143, done.
remote: Counting objects: 100% (143/143), done.
remote: Compressing objects: 100% (81/81), done.
remote: Total 143 (delta 25), reused 120 (delta 11), pack-reused 0
Receiving objects: 100% (143/143), 79.43 KiB | 191.00 KiB/s, done.
Resolving deltas: 100% (25/25), done.
thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ cd xjc_generated-code/
thufir@dur:~/NetBeansProjects/xjc_generated-code$ 
thufir@dur:~/NetBeansProjects/xjc_generated-code$ gradle clean run

> Task :run
Jan 17, 2019 2:34:06 PM xjc_generated.code.driver.App unmarshallMLI
INFO: generated.MLIspec@12edcd21

BUILD SUCCESSFUL in 1s
4 actionable tasks: 3 executed, 1 up-to-date
thufir@dur:~/NetBeansProjects/xjc_generated-code$ 
thufir@dur:~/NetBeansProjects/xjc_generated-code$ cat /home/thufir/xml/output.xml 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MLIspec>
    <id>4050response</id>
    <typename>4050Response</typename>
    <description>Show the Contract Numbers</description>
    <element>
        <id>7504</id>
        <name>SourceAccountIdentifier</name>
        <type>table</type>
        <required>true</required>
    </element>
</MLIspec>
thufir@dur:~/NetBeansProjects/xjc_generated-code$ 

即使在 Eclipse 中也应该可以工作。请注意,它使用 gradle 或 Gradle Kotlin DSL 作为构建文件,Eclipse 应该能够处理。可能您想作为 gradle 项目导入。

您只需要更改 properties.xml 文件 for your system;源代码应该没问题。