I have created an XML file and DTD which can be found at the HERE.
I have written a code, but it works till one level, then it doesnot works properly. I have also created certain objects to store the value of the xml file. But i am only able to traverse till sheet tag of the xml, then it doesnot works properly.
Recon recon = new Recon();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(configFile);
doc.getDocumentElement().normalize();
System.out.println("Root Element : " + doc.getDocumentElement().getNodeName());
String outputPath = doc.getDocumentElement().getAttribute("outputPath");
String withCompareFilePath = doc.getDocumentElement().getAttribute("withCompareFile");
String toCompareFilePath = doc.getDocumentElement().getAttribute("toCompareFile");
recon.setOutputPath(outputPath);
recon.setToCompareFile(new File(toCompareFilePath));
recon.setWithCompareFile(new File(withCompareFilePath));
NodeList sheetNodeList = doc.getElementsByTagName("sheet");
List<ReconSheet> reconSheets = new ArrayList<ReconSheet>();
for(int i = 0; i< sheetNodeList.getLength() ; i++) {
Node tempNode = sheetNodeList.item(i);
ReconSheet reconSheet = new ReconSheet();
NamedNodeMap attMap = tempNode.getAttributes();
Node sheetNode = attMap.getNamedItem("sheetNumber");
String sheetNumber = sheetNode.getNodeValue();
reconSheet.setSheetNumber(Integer.parseInt(sheetNumber));
NodeList list = tempNode.getChildNodes();
for(int j = 0; j< list.getLength(); j++) {
Node inNode = list.item(j);
System.out.println(inNode);
}
}
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
You could map your XML directly to your domain model using a JAXB implementation. JAXB requires Java SE 5, and a JAXB implementation is included in Java SE 6.
Recon
Your
Reconclass would look something like:ReconSheet
FileAdapter
Since a JAXB implementation can not interact with a
java.io.Fileobject directly we will use a JAXB adapter to handle this conversion. The use of this adapter is specified on theReconclass using the@XmlJavaTypeAdapterannotation:Demo
Output