package slipapp; import java.io.IOException; import java.util.Date; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * * @author Doys */ public class XmlHandle { private Document document; public int rowCount; public XmlType xmlType = null; private String doubleQuote = ""; private String singleQuote = ""; public XmlHandle(String urlXml){ init(urlXml); } public XmlHandle(String xmlUrl, String testXmlUrl, RunMode mode) { if (mode == RunMode.TEST) { init(testXmlUrl); } else { init(xmlUrl); } } public void init(String xmlUrl) { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = builderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { System.out.println("error_00 " + e); } try { //WebClient https = new WebClient(); //String xmlString = https.getContent(xmlUrl); //InputSource is = new InputSource(new StringReader(xmlString)); document = builder.parse(xmlUrl); } catch (SAXException | IOException ex) { document = null; System.out.println("error_01 " + ex); } } public void setRowCount(String primaryName) { String primary = this.xmlType == XmlType.DATATABLE ? primaryName.toUpperCase() : primaryName.toLowerCase(); NodeList list = document.getElementsByTagName(primary); this.rowCount = list.getLength(); } public int getRowCount() { return this.rowCount; } public String getItemString(int row, String column) throws Exception { column = column.toLowerCase(); NodeList list = document.getElementsByTagName(column); Element el = (Element) list.item(row); String nodeValue = el.getFirstChild().getNodeValue(); return nodeValue; } public String getItemStringTryCatch(int row, String column) { return getItemStringTryCatch(row, column, ""); } public String getItemStringTryCatch(int row, String column, String catchResult) { try { return getItemString(row, column); } catch (Exception ex) { return catchResult; } } public int getItemInt(int row, String column) throws Exception { return Integer.parseInt(this.getItemString(row, column)); } public int getItemIntTryCatch(int row, String column) { return getItemIntTryCatch(row, column, 0); } public int getItemIntTryCatch(int row, String column, int catchResult) { try { return getItemInt(row, column); } catch (Exception ex) { return catchResult; } } public double getItemDouble(int row, String column) throws Exception { return Double.parseDouble(this.getItemString(row, column)); } public double getItemDoubleTryCatch(int row, String column) { return getItemDoubleTryCatch(row, column, 0); } public double getItemDoubleTryCatch(int row, String column, double catchResult) { try { return getItemDouble(row, column); } catch (Exception ex) { return catchResult; } } public float getItemFloat(int row, String column) throws Exception { return Float.parseFloat(this.getItemString(row, column)); } public float getItemFloatTryCatch(int row, String column) { return getItemFloatTryCatch(row, column, 0); } public float getItemFloatTryCatch(int row, String column, float catchResult) { try { return getItemFloat(row, column); } catch (Exception ex) { return catchResult; } } public Date getItemDate(int row, String column) throws Exception { return new Date(); } public Date getItemDateTryCatch(int row, String column) { return getItemDateTryCatch(row, column, new Date(1700, 0, 1)); } public Date getItemDateTryCatch(int row, String column, Date catchResult) { try { return getItemDate(row, column); } catch (Exception ex) { return catchResult; } } }