我已经创建了一个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