java JAXP如何获取数据类型的属性?

时间:2011-04-21 07:02:59

标签: xml xsd jaxp

我正在使用JAXP使用XSD解析和验证XML。我能够做到这一点。我需要存储属性的数据类型。有没有办法使用JAXP?

我的代码如下......

    SchemaFactory factory = SchemaFactory.newInstance(language);
        schema = factory.newSchema(new StreamSource("test.xsd"));

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setSchema(schema);

        DocumentBuilder db =dbf.newDocumentBuilder();
        db.setErrorHandler(new DefaultHandler());

        Document dom = db.parse(new File("test.xml"));

        Element root = dom.getDocumentElement();

        System.out.println("Node Name: "  + root.getNodeName()+ "  Node Value: " + root.getNodeValue());
        TypeInfo type =root.getSchemaTypeInfo();

        System.out.println("Root : " + type + type.getTypeName());
        Attr a =root.getAttributeNode("orderid");

        type = a.getSchemaTypeInfo();
        System.out.println("Attr : " + type.toString() + " Type:  " + type.getTypeName());

XML输入:

<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="889923">
  <orderperson>John Smith</orderperson>
  <shipto>
    <name>Ola Nordmann</name>
    <address>Langgt 23</address>
    <city>4000 Stavanger</city>
    <country>Norway</country>
  </shipto>
  <item>
    <title>Empire Burlesque</title>
    <note>Special Edition</note>
    <quantity>1</quantity>
    <price>10.90</price>
  </item>
  <item>
    <title>Hide your heart</title>
    <quantity>1</quantity>
    <price>9.90</price>
  </item>
</shiporder>

XSD:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="shiporder">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="orderperson" type="xs:string"/>
      <xs:element name="shipto">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="address" type="xs:string"/>
            <xs:element name="city" type="xs:string"/>
            <xs:element name="country" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="item" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="note" type="xs:string" minOccurs="0"/>
            <xs:element name="quantity" type="xs:positiveInteger"/>
            <xs:element name="price" type="xs:decimal"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>

</xs:schema>

我得到的输出是:

Node Name: shiporder  Node Value: null
Root : [shiporder: null]null
Attr : orderid="889923" type  null

2 个答案:

答案 0 :(得分:2)

如果您正在使用SAX解析(ValidatorHandler),则需要ValidatorHandler.getTypeInfoProvider().getAttributeTypeInfo()。如果您正在使用DOM解析,则需要Attr.getSchemaTypeInfo()

答案 1 :(得分:0)

从@ muni上面的示例代码开始,他已经调用了a.getSchemaTypeInfo(),它仍然以null作为我的类型返回。

添加dbf.setNamespaceAware(true);为我打印了类型信息。