有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java解析XSD以列出元素

我一直在尝试解析一个(实际上是多个)xsd文件,以写出元素名称、各自的元素类型和文档的列表

我研究了XSOM、SAXParser、Xerces和JAXP——所有这些都使读取xml和读取节点变得容易。在不等同于元素名的情况下读取xsd(获取所有元素名的列表)似乎很困难。 解析器。parse可以很好地处理我尝试的大多数lib(因为XSD是一种格式良好的xml),但我无法超越它(提取所有元素名)

我遗漏了什么吗?有人有过类似问题的经历吗

以下是一个示例xsd:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://abc.mycompany.com/dto/address" targetNamespace="http://abc.mycompany.com/sdo/address">
  <xs:complexType name="Address">
    <xs:sequence> 
      <xs:element name="address1" minOccurs="0">
        <xs:annotation>
          <xs:documentation>USPS standardized address: building number, street name, apartment/suite number, and directionals (e.g., NE, SE, NW, SW).</xs:documentation>
        </xs:annotation>
        <xs:simpleType>
          <xs:restriction base="xs:normalizedString">
            <xs:maxLength value="100" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="address2" minOccurs="0">
        <xs:annotation>
          <xs:documentation>Additional field for wrapping long addresses.</xs:documentation>
        </xs:annotation>
        <xs:simpleType>
          <xs:restriction base="xs:normalizedString">
            <xs:maxLength value="100" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="city" minOccurs="0">
        <xs:annotation>
          <xs:documentation>Name of the city, town or village.</xs:documentation>
        </xs:annotation>
        <xs:simpleType>
          <xs:restriction base="xs:normalizedString">
            <xs:maxLength value="26" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="state" type="xs:normalizedString" minOccurs="0" >
        <xs:annotation>
          <xs:documentation>A pick list of two-letter abbreviations representing US states,
                              military post offices, US protectorates, and Canadian provinces.
      </xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:element name="zipCode" type="xs:normalizedString" minOccurs="0" >
        <xs:annotation>
          <xs:documentation>The first 5 digits of a 9-digit (Zip+4) zip code,
                    used to geographically locate a US address.</xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

共 (1) 个答案

  1. # 1 楼答案

    它必须是一个配置为处理名称空间的解析器

    您还可以使用XSL-T匹配“xs:element”并以这种方式提取名称