0

I am new to JAXB and it seems to be pretty straightforward but I have this XML (at the bottom of this post) and I want to map it to one "StorageArray" java object. Each StorageArray object will have many "HostStorageDomain" objects and each one of those may have zero to many "WWN" objects.

Another issue that I may have is, I already have the fields set from a different source for the "StorageArray" class. Everything except the HostStorageDomain and WWN is present, so I don't really need to map StorageArray but I do need the children elements since that is new information I want to map. I'm pulling the storagearray information from a CSV and when I start parsing this XML that data will be populated (and it has other data too). So I don't know if I need to modify the StorageArray class with JAXB annotations?

Also, Do I need to make a class for HostStorageDomain AND WWN? I don't care about the DvMCLIResult or CommandResult.

<?xml version="1.0" encoding="UTF-8"?>
<DvMCLIResult version="7.4.0-00">
<CommandResult command="GetSystemData">
<StorageArray objectID="ObjectID1" name="name1" description="Description1" serialNumber="87010488" arrayFamily="arrayModle" arrayType="arrayType" productName="productName" controllerVersion="controllerVersion" numberOfControllers="2" capacityInGB="96287" distributedMode="-1" >
                <HostStorageDomain objectID="HSDOMAINID1" portID="0" portName="portName1" domainID="0" hostMode="Standard" hostMode2="" displayName="portName1" domainType="1" iSCSIName="iSCSIName1" nickname="nickname1" />
                <HostStorageDomain objectID="HSDOMAINID2" portID="1" portName="portName2" domainID="0" hostMode="Standard" hostMode2="" displayName="portName2" domainType="1" iSCSIName="iSCSIName2" nickname="nickname2" >
                                <WWN nickname="WWName1" WWN="123456"/>
                                <WWN nickname="WWName2" WWN="234567"/>
                                <WWN nickname="WWName3" WWN="346578"/>
                </HostStorageDomain>
                <HostStorageDomain objectID="HSDOMAINID3" portID="16" portName="portName3" domainID="0" hostMode="Standard" hostMode2="" displayName="portName3" domainType="1" iSCSIName="iSCSIName3" nickname="nickname3" />
                <HostStorageDomain objectID="HSDOMAINID4" portID="20" portName="portName4" domainID="0" hostMode="Standard" hostMode2="" displayName="portName2" domainType="0" nickname="nickname4" >
                    <WWN nickname="WWName4" WWN="342523"/>
                </HostStorageDomain>
</StorageArray>

</CommandResult>
</DvMCLIResult>

EDIT:

Following Stephen Carlsons answer -- which seems to be working and I will accept is that I'm getting an error:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"DvMCLIResult"). Expected elements are <{}HostStorageDomain>,<{}Path>,<{}StorageArray>,<{}WWN>

I have a storage class that starts like this and contains the HostStorageDomain Class -- StorageDomains

@XmlRootElement(name = "StorageArray")
@XmlAccessorType(XmlAccessType.NONE) // Only specifying the fields I want with annotations
public class Storage implements Serializable {
   ...
   @XmlElement(name = "HostStorageDomain")
   List<StorageDomain> domains = new ArrayList<StorageDomain>();

In a translator class, I am performing the unmarshalling like this

JAXBContext context = JAXBContext.newInstance(Storage.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            storage = (Storage) unmarshaller.unmarshal(is); // Inputstream of the xml String

Any ideas?

Envin
  • 1,463
  • 8
  • 32
  • 69
  • 1
    My xmllint says your XML is not valid, maybe fellows will try to indent it to understand the structure perfectly. – gyorgyabraham May 28 '13 at 15:37
  • The XML is A LOT bigger so I shortened it...I'll double check to see if I left anything crucial out. – Envin May 28 '13 at 17:05
  • 1
    Check question http://stackoverflow.com/questions/16689355/how-to-read-this-xml-file-in-java-need-to-be-done-by-using-exported-firstname-l/16690668#16690668, which I had anwsered. It may help you – Karthik Prasad May 29 '13 at 05:19

1 Answers1

1

You'll need to create classes for HostStorageDomain and WWN, as well as annotate StorageArray. HostStorageDomain and WWN will look similar:

@XmlRootElement(name="HostStorageDomain")
public class HostStorageDomain
{
    @XmlElement
    public WWN[] WWN_list;

    @XmlAttribute(name="objectID")
    public String ObjectID;

    @XmlAttribute(name="portID")
    public String PortID;

    ...
}

StorageArray will look like the following. You can skip annotating the other properties if you don't need them bound. However, after unmarshalling you will have to reconcile the instance created by the unmarshalling process with whatever you have in memory already:

@XmlRootElement(name="StorageArray")
public class StorageArray
{
    @XmlElement
    public HostStorageDomain[] Domains;
}

Hope that helps!

Steve

Stephen Carlson
  • 276
  • 1
  • 9