针对通过 Eclipse 中的 JAXB 和 XSD 构建选项生成的 xsd 文件验证 xml [英] Validating xml against xsd file generated through JAXB and XSD build option in eclipse

查看:38
本文介绍了针对通过 Eclipse 中的 JAXB 和 XSD 构建选项生成的 xsd 文件验证 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to have a sample code for validating my xml file against xsd file, which is getting generated through jaxb and xsd build through Eclipse.

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

I am trying to do a sample validator file, that validates my xml against my xsd file. Now, I have found a lot of examples for the java code, and I am using one of them. Also I have recieved a lot of answers for the issue that I am facing, but some way the answers suggests to either update my xsd or xml file. For me, I cannot do any of these, as I am generating the xsd files through jaxb and xsd build option through Eclipse.

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)类型定义"组件.

Below is the error that I am receiving Exception: src-resolve: Cannot resolve the name 'SourceAccountIdentifier' to a(n) 'type definition' component.

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

Also, I tried to remove SourceAccountIdentifier from my xsd definition, to see if it fails anywhere else, in that case, I recieve the below error

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

请帮助提供一些错误的解决方案.谢谢!!

Please help to provide some solution for the error. Thanks!!

推荐答案

生成的代码在 github 给你.

The generated code is on github for you.

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

I'm gussing that it's a problem with the xml or xsd files -- but I don't know for a fact.

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

I removed the dtd for now, here's what I have so far:

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 上. 下面是如何运行该项目,它从控制台构建:

The tools are trang and xjc which install on Ubuntu Linux easily enough. Here's how to run the project, which builds from the console:

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 项目导入.

That should work even from Eclipse. Note that it's using gradle, or the Gradle Kotlin DSL for the build file, which Eclipse should be able to handle. Probably you want to import as a gradle project.

您应该只需要更改 properties.xml 文件 适用于您的系统;源代码应该没问题.

You should only have to alter the properties.xml file for your system; the source code should be fine.

这篇关于针对通过 Eclipse 中的 JAXB 和 XSD 构建选项生成的 xsd 文件验证 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆