解析XML模式中的类型。 Java的

时间:2012-10-09 05:27:30

标签: java xml xsd jaxb

在我面前有一个问题 - 一个完整的模式解析(包含,导入,重新定义)。问题,甚至不是解析方案,它是从这个模式解析类型。 为了解释这个问题,这是一个例子:

schema.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myprefix="http://myhost.ru/service/" xmlns:anprefix="http://anotherhost.ru/pub-service/" targetNamespace="http://myhost.ru/service/">
    <xs:import namespace="http://anotherhost.ru/pub-service/" schemaLocation="anotherhost_schema.xsd"/>

    <xs:complexType>
        <xs:sequence>
            <xs:element name="ServiceCode" type="anprefix:ServiceCodeType">
                <xs:annotation>
                    <xs:documentation>Service code</xs:documentation>
                </xs:annotation>
            </xs:element>
            <xs:element name="MessageClass" type="myprefix:MessageClassType"/>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="MessageClassType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="REQUEST">
                <xs:annotation>
                    <xs:documentation>Request from client
                    </xs:documentation>
                </xs:annotation>
            </xs:enumeration>
            <xs:enumeration value="RESPONSE">
                <xs:annotation>
                    <xs:documentation>Response to client</xs:documentation>
                </xs:annotation>
            </xs:enumeration>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

anotherhost_schema.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:anprefix="http://anotherhost.ru/pub-service/" targetNamespace="http://anotherhost.ru/pub-service/">
    <xs:simpleType name="ServiceCodeType">
        <restriction base="xs:string">
        </restriction>
    </xs:simpleType>
</xs:schema>

示例并不是很困难,真正的问题要大得多。我的问题是解析模式并创建它的内部表示(表单生成器和请求处理程序将使用),例如,如下所示:

{
    "ServiceCode": {"string", String.class, "Service code"},
    "MessageClass": {"string", {"REQUEST":"Request from client","RESPONSE":"Response to client"}, ""}
}

内部表示可以是各种各样的,但在Java中使用很简单。有这样的图书馆吗?我知道,有一个名为JAXB的库,专门用于XML解析,但我不明白如何将它绑定到我的问题。

0 个答案:

没有答案