Reputation: 49
I need to parse the following XSD snippet
<xs:element name="BANT" type="tns:B"/>
<xs:complexType name="InqRq_Type">
<xs:sequence>
<xs:element name="Header" type="tns:Rq"/>
<xs:element name="Data" type="tns:InqRqData_Type"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="RqHeader_Type">
<xs:sequence>
<xs:element name="Filler1" minOccurs="0" >
<xs:simpleType >
<xs:restriction base="xs:string">
<xs:maxLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="MsgLen" minOccurs="0" >
<xs:simpleType >
<xs:restriction base="xs:string">
<xs:maxLength value="4"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Filler2" minOccurs="0" >
<xs:simpleType >
<xs:restriction base="xs:string">
<xs:maxLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="MsgType" minOccurs="0" >
<xs:simpleType >
<xs:restriction base="xs:string">
<xs:maxLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Filler3" minOccurs="0" >
<xs:simpleType >
<xs:restriction base="xs:string">
<xs:maxLength value="4"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="CycleNum" minOccurs="0" >
<xs:simpleType >
<xs:restriction base="xs:string">
<xs:maxLength value="6"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="MsgNum" minOccurs="0" >
<xs:simpleType >
<xs:restriction base="xs:string">
<xs:maxLength value="6"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
If I parse it using a SAX parser I'm having difficulty parsing the complexType.
Should be using a DOM parser or is there a way to parse complexType easily using the SAX parser.
Difficulty : -
NodeList list = doc.getElementsByTagName("xs:element");
gives me all the elements even the ones under the complexType. So, how do I take the complexType and store the elements under it and then process the rest of the information.
Upvotes: 0
Views: 381
Reputation: 1753
Using DOM Parser,
NodeList cL = doc.getElementsByTagName("xs:complexType");
Node cN= cL.item(0);
NodeList cNC= cN.getChildNodes();
Element cE =(Element) cNC;
NodeList eL=cE.getElementsByTagName("xs:element");
for (int i = 0; i < eL.getLength(); i++)
{
Node eN = eL.item(i); // ith element
NodeList eNC= eN.getChildNodes();
Element eE= (Element) eNC;
//extract elements values here or loop other nodes like this
}
DOM loads entire xml as tree structure in memory to parse while SAX uses event based parsing so SAX gives good performance over DOM. For small xml like this you can use DOM, whichever comes easy to you.
Upvotes: 2