我已經創建了一個XML文件和DTD,可以在HERE找到。如何閱讀Java中的XML文件的內容
我已經寫了一個代碼,但它的工作,直到一個級別,然後它不能正常工作。我還創建了某些對象來存儲xml文件的值。但我只能遍歷到xml的sheet
標記,那麼它不能正常工作。
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);
}
}
@oers:使用該適配器的使用
@XmlJavaTypeAdapter
註解放在Recon
類指定需要編輯。 M.J,下次請將您的代碼示例包含在問題本身中。 – Perception