I am developing a xml mapper in java. I use Eclipse Moxy for it an I faced a problem in the middle of it. I can get all the child nodes of a parent node in to a hashmap. But the problem is the attributes of the parent node also go in to that hashmap. But I want to get those attributes separately. Following is my code
I have following xml segment
<keystore name="xyz">
<type>JKS</type>
<password>wso2carbon</password>
<keyAlias>wso2carbon</keyAlias>
<keyPassword>wso2carbon</keyPassword>
</keystore>
Following is the related XSD part
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="keystore">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="type"/>
<xs:element type="xs:string" name="password"/>
<xs:element type="xs:string" name="keyAlias"/>
<xs:element type="xs:string" name="keyPassword"/>
</xs:sequence>
<xs:attribute type="xs:string" name="name"/>
</xs:complexType>
</xs:element>
</xs:schema>
I want to map this XML to in my java code. When I access it using Moxy I get "name" attribute also in the map of the child elements. Ex:
[0]name
[1]type
[2]password
........
But I want to get the attribute of the parent element ("name") separately(not in the child elements map). Can I get the attributes of a xml elements separately ? ?
This is how my code look like
private DynamicEntity getDynamicEntity()
throws SAXException, URISyntaxException, IOException, XMLStreamException,
ConfigurationMismatchException {
FileInputStream xsdInputStream = null;
DynamicJAXBContext jaxbContext;
DynamicEntity autoElement = null;
try {
xsdInputStream = new FileInputStream("/home/....../automation_mapping.xsd");
FileInputStream xmlInputStream = new FileInputStream("/home/...../automation.xml");
jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
autoElement = (DynamicEntity) unmarshaller.unmarshal(xmlInputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return autoElement;
}
This autoElement object contains the xml file data. then I access the DynamicEntity using following code
DynamicEntity myEntity=autoelment.get(name);
But the problem is the attribute of the elements cannot get separately using get() method