package reportbuilder; import java.io.File; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class XmlParserReport { private Document doc; private NodeList nList; public XmlParserReport(String xml) throws Exception { this.doc = loadXMLFromString(xml); String tagRowName = "report_row"; try{ tagRowName=doc.getFirstChild().getChildNodes().item(1).getNodeName(); }catch(Exception e){ tagRowName = "report_row"; } nList = doc.getElementsByTagName(tagRowName); } public XmlParserReport(File xml) throws Exception { this.doc = loadXMLFromFile(xml); String tagRowName = "report_row"; try{ tagRowName=doc.getFirstChild().getChildNodes().item(1).getNodeName(); }catch(Exception e){ tagRowName = "report_row"; } nList = doc.getElementsByTagName(tagRowName); } public String getItemString(int row, String column) { try { Node n = nList.item(row); for (int i = 0; i < n.getChildNodes().getLength(); i++) { if (n.getChildNodes().item(i).getNodeName().toLowerCase().trim().equals(column.trim())) { return n.getChildNodes().item(i).getTextContent(); } } } catch (Exception ex) { } return ""; } public int getRowCount() { return nList.getLength(); } private Document loadXMLFromString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); } private Document loadXMLFromFile(File xml) throws Exception { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); return dBuilder.parse(xml); } public static void main(String[]args){ try{ String xml="รายงานกำหนดสิทธ์การเข้าใช้งานหน้าจอของระบบต่างๆ D:\\ICOOP_ALL\\ICORE\\ICOOP\\iReport\\Reports\\admin_wins_report_permiss.jasperD:\\ICOOP_ALL\\IEXT\\ICOOP\\Saving\\IREPORTFILE\\20160427152640604636_000_r_admin_wins_report_permiss.pdfhttp://localhost:80/IEXT/ICOOP/Saving/IREPORTFILE/20160427152640604636_000_r_admin_wins_report_permiss.pdfpdfutf-8"; XmlParserReport xml_=new XmlParserReport( xml); }catch(Exception e){ e.printStackTrace(); } } }