Search code examples
xmlxsdjaxp

java JAXP how to get the data type of attributes?


I am using JAXP to parse and validate XMLs using XSD. I am able to do that. I need to store the data type of attributes. Is there any way to do so using JAXP?

My code Is as below...

    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 Input:

<?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>

Output I am getting is:

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

Solution

  • If you're using SAX parsing (ValidatorHandler), you want ValidatorHandler.getTypeInfoProvider().getAttributeTypeInfo(). If you're using DOM parsing you want Attr.getSchemaTypeInfo().