XML Parsing With JAXB



I want to parse the following xml, i tried with JAXB as follows,



public class XMLParsing {

private static Fields fields = null;
private static JAXBContext context = null;
private static Unmarshaller unmarshaller = null;

public static void main(String[] args) {

fields = process();

System.out.println("Field List : "+fields.getFieldList());

}

public static Fields process(){

try{
context = JAXBContext.newInstance(Fields.class);
unmarshaller = context.createUnmarshaller();
fields = (Fields) unmarshaller.unmarshal(new File("/media/rakesh/WorkZ/test.xml"));
} catch(Exception e){
e.printStackTrace();
} finally{
context = null;
unmarshaller = null;
}

return fields;

}

}


XML File :



<?xml version="1.0" encoding="UTF-8"?>
<Fields>
<Field Name="list">
<Elements>
<Element Index="0">
<Fields>
<Field Name="sin" Value="20"/>
<Field Name="propList">
<Elements>
<Element Index="0">
<Fields>
<Field Name="pin" Value="1"/>
<Field Name="value" Value="0" Type="enum"/>
</Fields>
</Element>
<Element Index="1">
<Fields>
<Field Name="pin" Value="6"/>
<Field Name="value" Value="2448900" Type="signedint"/>
</Fields>
</Element>
<Element Index="2">
<Fields>
<Field Name="pin" Value="7"/>
<Field Name="value" Value="-4394255" Type="signedint"/>
</Fields>
</Element>
<Element Index="3">
<Fields>
<Field Name="pin" Value="9"/>
<Field Name="value" Value="1" Type="unsignedint"/>
</Fields>
</Element>
</Elements>
</Field>
</Fields>
</Element>
<Element Index="1">
<Fields>
<Field Name="sin" Value="27"/>
<Field Name="propList">
<Elements>
<Element Index="0">
<Fields>
<Field Name="pin" Value="1"/>
<Field Name="value" Value="IsatData Pro - Modem" Type="string"/>
</Fields>
</Element>
<Element Index="1">
<Fields>
<Field Name="pin" Value="3"/>
<Field Name="value" Value="01074056SKYFFE5" Type="string"/>
</Fields>
</Element>
<Element Index="2">
<Fields>
<Field Name="pin" Value="4"/>
<Field Name="value" Value="1074056" Type="unsignedint"/>
</Fields>
</Element>
</Elements>
</Field>
</Fields>
</Element>
</Elements>
</Field>
</Fields>


Fields.class



@XmlRootElement(name="Fields")
public class Fields {

private List<Field> fieldList;

/**
* @return the fieldList
*/
public List<Field> getFieldList() {
return fieldList;
}

/**
* @param fieldList the fieldList to set
*/
@XmlElement(name="FieldList")
public void setFieldList(List<Field> fieldList) {
this.fieldList = fieldList;
}

}


I think the problem is with the Fields class. Can any please suggest the correct way to parse the xml.


No comments:

Post a Comment