Cannot get Attributes separately in the xml file in EclipseLink - Moxy

298 Views Asked by At

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

1

There are 1 best solutions below

1
On

A DynamicEntity corresponds to a Java class from your domain model. The reason that you can access properties that map to attributes and elements in the same manner:

keyStoreDE.get("name");
keyStoreDE.get("type');

Is that in the real POJO (see below) you would be able to access them in the same manner:

keyStorePOJO.getName();
keyStorePOJO.getType();
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "type",
    "password",
    "keyAlias",
    "keyPassword"
})
@XmlRootElement(name = "keystore")
public class Keystore {

    @XmlElement(required = true)
    protected String type;
    @XmlElement(required = true)
    protected String password;
    @XmlElement(required = true)
    protected String keyAlias;
    @XmlElement(required = true)
    protected String keyPassword;
    @XmlAttribute(name = "name")
    protected String name;

    // get/set methods
}