1 package org.webdocwf.util.xml;
2
3 //xml imports
4 import org.w3c.dom.Document;
5 import org.w3c.dom.NodeList;
6 import org.w3c.dom.Node;
7 import org.w3c.dom.Element;
8 import org.enhydra.xml.*;
9 import javax.xml.parsers.DocumentBuilder;
10 import javax.xml.parsers.DocumentBuilderFactory;
11 //XMLdriver imports
12 import org.enhydra.xml.SearchElement;
13
14 import java.sql.*;
15 import java.io.File;
16 import java.util.ArrayList;
17
18 /***
19 * Class load existing XML file , creating DOM from file or creating
20 * new DOM.Methods will change DOM and save new DOM in XML file.
21 *
22 * @version 1.0
23 * @author Zoran Milakovic
24 */
25
26 public class XmlReader
27 {
28 private String[] columnNames;
29 private String[] columnValues;
30 private String tableName;
31
32 /***
33 * Document made from XML file, and in which will
34 * be made changes.Document will be saved in XML file.
35 */
36 private SearchElement searchDocument;
37 private Document document;
38 /***
39 * Full path of the XML file.
40 */
41 private String fileName;
42 /***
43 * Constructor will build Document from the specified file
44 * if file exist, or will create new Document if file not exist.
45 *
46 * @param fileName full pathname of the XML file
47 * @throws SQLException
48 */
49 public XmlReader(String fileName) throws SQLException {
50 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
51 try {
52 this.fileName = fileName;
53 File file = new File( fileName );
54 DocumentBuilder builder = factory.newDocumentBuilder();
55 try {
56 this.document = builder.parse( file );
57 } catch( Exception e ) {
58 throw new SQLException("Error while parsing XML file ! : "+e.getMessage());
59 }
60 this.searchDocument = (SearchElement)SearchElement.newInstance( document );
61 } catch( Exception e ) { throw new SQLException("Error in creating DOM : "+e.getMessage()); }
62 }
63
64
65
66
67 private ArrayList rset = new ArrayList();
68
69 /***
70 * Gets data from database.Method will fill array list which will be result set.
71 * ArrayList will contain arrays of strings.Every array of string will present
72 * one row in database.
73 *
74 * @param tableName Name of table.
75 * @param columnNames Names of columns from which will be select data.
76 * @param whereColumnNames Names of columns in where conditions.
77 * @param whereColumnValues Values of conditions.
78 * @throws SQLException
79 */
80 public void select(String tableName , String[] columnNames , String[] whereColumnNames , String[] whereColumnValues) throws SQLException {
81
82 NodeList tableRows = searchDocument.getSubElementsByTagName("dml/"+tableName);
83 for(int i = 0; i < tableRows.getLength(); i++) {
84 boolean isMatch = true;
85 if( whereColumnNames != null && whereColumnValues != null ) {
86 for(int k = 0; k < whereColumnNames.length; k++) {
87 NodeList columns = ( (SearchElement)tableRows.item(i) ).getSubElementsByCondition(whereColumnNames[k]+"="+whereColumnValues[k]);
88 if( columns.getLength() == 0 )
89 isMatch = false;
90 }
91 }
92 if( isMatch ) {
93 ArrayList colValuesList = new ArrayList();
94 colValuesList.clear();
95 for(int k = 0; k < columnNames.length; k++) {
96 NodeList columns = ( (SearchElement)tableRows.item(i) ).getSubElementsByTagName(columnNames[k]);
97 Node column = columns.item(0);
98 Node textNode = column.getFirstChild();
99 if( textNode == null )
100 colValuesList.add( "null" );
101 else
102 colValuesList.add( textNode.getNodeValue() );
103 }
104 rset.add( colValuesList.toArray(new String[0]) );
105 int y = 0;
106 }
107 }
108 }
109
110 public ArrayList getResultSet() {
111 return this.rset;
112 }
113
114 }
115
This page automatically generated by Maven