This is the XML I am using to parse.
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
and this is the schema for it.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="title"/>
<xs:element type="xs:string" name="author"/>
<xs:element type="xs:short" name="year"/>
<xs:element type="xs:float" name="price"/>
</xs:sequence>
<xs:attribute type="xs:string" name="category" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I have written code using javax.xml library to validate and parse it and I am able to get all the element names from the XSD. Here I am facing some difficulty in getting attributes's element name.
For an instance, in the above XSD we have an attribute called category and here I want to get it's element name nothing but book(We can find it in XML file and I couldn't see any relation that this is the attribute and it's element is so and so). How can I get this value? Finally I just want to form a string something like this book.category.
Can anyone please suggest me how can I form this String? Thanks In Advance.
Code used to get an attribute name category.
val attrList = doc.getElementsByTagName("xs:attribute")
val attrName = attrList.item(0).getAttributes.item(0).getNodeValue