/* 431: */ /* 432: */ protected void entityResolver(Document document) /* 433: */ throws SAXException /* 434: */ { /* 435:687 */ if (this.entityResolver != null) /* 436: */ { /* 437:688 */ DocumentType docType = document.getDocType(); /* 438:690 */ if (docType != null) /* 439: */ { /* 440:691 */ String publicID = docType.getPublicID(); /* 441:692 */ String systemID = docType.getSystemID(); /* 442:694 */ if ((publicID != null) || (systemID != null)) { /* 443: */ try /* 444: */ { /* 445:696 */ this.entityResolver.resolveEntity(publicID, systemID); /* 446: */ } /* 447: */ catch (IOException e) /* 448: */ { /* 449:698 */ throw new SAXException( "Could not resolve publicID: " + publicID + " systemID: " + systemID, e); /* 450: */ } /* 451: */ } /* 452: */ } /* 453: */ } /* 454: */ }
/* 406: */ /* 407: */ protected void documentLocator(Document document) /* 408: */ throws SAXException /* 409: */ { /* 410:661 */ LocatorImpl locator = new LocatorImpl(); /* 411: */ /* 412:663 */ String publicID = null; /* 413:664 */ String systemID = null; /* 414:665 */ DocumentType docType = document.getDocType(); /* 415:667 */ if (docType != null) /* 416: */ { /* 417:668 */ publicID = docType.getPublicID(); /* 418:669 */ systemID = docType.getSystemID(); /* 419: */ } /* 420:672 */ if (publicID != null) { /* 421:673 */ locator.setPublicId(publicID); /* 422: */ } /* 423:676 */ if (systemID != null) { /* 424:677 */ locator.setSystemId(systemID); /* 425: */ } /* 426:680 */ locator.setLineNumber(-1); /* 427:681 */ locator.setColumnNumber(-1); /* 428: */ /* 429:683 */ this.contentHandler.setDocumentLocator(locator); /* 430: */ }
/** * 将excel内的内容读取到xml文件中,并添加dtd验证 * * @param xmlFile * @param sheetNum * @return 1代表成功,0失败,-1超过最大sheet,2跳过当前失败的xml */ public int excelToXml(String xmlFile, int sheetNum) { if (sheetNum >= workBook.getNumberOfSheets()) return -1; else sheet = workBook.getSheetAt(sheetNum); xmlFile = xmlFile + ".xml"; try { Document document = DocumentHelper.createDocument(); // 使用sheet名称命名跟节点 String rootName = sheet.getSheetName().replaceAll(" ", ""); Element root = document.addElement(rootName); // 添加dtd文件说明 DocumentType documentType = new DOMDocumentType(); documentType.setElementName(rootName); List<ElementDecl> declList = new ArrayList<>(); declList.add(new ElementDecl(rootName, "(row*)")); // 判断sheet是否为空,为空则不执行任何操作 if (sheet.getRow(0) == null) return 1; // 遍历sheet第一行,获取元素名称 row = sheet.getRow(0); String rowString = null; List<String> pcdataList = new ArrayList<>(); for (int y = 0; y < row.getPhysicalNumberOfCells(); y++) { Object object = this.getCellValueObject(0, y); // 判断是否有合并单元格,有的话跳过 if (object == null) return 2; // 去除表头字符串中的空格 String objectStr = object.toString().replaceAll(" ", ""); if (rowString != null) rowString += "|" + objectStr; else rowString = objectStr; pcdataList.add(objectStr); } // 设置行节点 declList.add(new ElementDecl("row", "(" + rowString + ")*")); // 遍历list设置行的下级节点 for (String tmp : pcdataList) { declList.add(new ElementDecl(tmp, "(#PCDATA)")); } documentType.setInternalDeclarations(declList); // 遍历读写excel数据到xml中 for (int x = 1; x < sheet.getLastRowNum(); x++) { row = sheet.getRow(x); Element rowElement = root.addElement("row"); for (int y = 0; y < row.getPhysicalNumberOfCells(); y++) { // cell = row.getCell(y); Object object = this.getCellValueObject(x, y); if (object != null) { // 将sheet第一行的行首元素当作元素名称 String pcdataString = pcdataList.get(y); Element element = rowElement.addElement(pcdataString); // Element element = rowElement.addElement("name"); element.setText(object.toString()); } } } // 写入文件和dtd document.setDocType(documentType); this.docToXmlFile(document, xmlFile); } catch (Exception e) { e.printStackTrace(); } return 1; }
protected void writeDocType(DocumentType docType) throws IOException { if (docType != null) { docType.write(writer); writePrintln(); } }