I have a xml to read and to create a list from that data similar to the hierarchy of the xml.According to the following code i was able to traverse through the whole xml.(Recursive method). Since the xml structure can change, i had to use to recursive method.
But my issue is im unable to create a list similar to the xml. idont know how to save the data of xml in a list. Thanks.
List<OMElement> parentList = new ArrayList<OMElement>();
List<OMElement> tempList = new ArrayList<OMElement>();
private void getChildrenRecursively(OMFactory fac, OMElement rootElement,Iterator childElement,List<OMElement> elementList) {
OMElement innerElementValue =null;
OMElement innerElement = null;
while (childElement.hasNext()) {
innerElement = (OMElement) childElement.next();
String innerElementLN = innerElement.getLocalName();
String innerElementText = innerElement.getText();
// Creates the new OMElement including the child element
innerElementValue = createNewOMElement(innerElementLN,innerElement.getAllDeclaredNamespaces(), innerElementText,fac);
// Get children recursively
if(innerElement.getChildElements().hasNext()){
// parentList.add(innerElementValue);
getChildrenRecursively(fac,rootElement,innerElement.getChildElements(),elementList);
}
else{
tempList.add(innerElementValue);
}
}
// TODO include for inside for two lists
for (int x = 0; x < tempList.size(); x++) {
if (rootElement.getFirstElement().getFirstElement() != null) {
rootElement.getFirstElement().getFirstElement().addChild(tempList.get(x));
}
}
// Creates a list of elements
elementList.add(rootElement);
}
Thanks.