001    import java.net.*; // URL, URLConnection */ 
002    import javax.xml.parsers.*; // Document Builder */ 
003    import java.io.*; // InputStream  
004    import org.w3c.dom.*; // Document, NodeList 
005    import javax.xml.transform.*; // Transformer, Transformer Factory 
006    import javax.xml.transform.dom.*; // DOMSource 
007    
008    /** 
009     A class used to provide methods 
010     for downloading and traversing XML pages. 
011     @author Matthew T Hough
012     @version 0.1 6/2/2006 
013     @see <br><a target="_blank"  
014     href="http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/TOC.html"> 
015     Working with XML by Sun Corporation</a> 
016     @see <br><a target="_blank"  
017     href="http://java.sun.com/j2se/1.5.0/docs/api/index.html"> 
018     Java 1.5 Documentation by Sun Corporation</a> 
019     @usage XMLRead xml = new XMLRead();      
020     */
021    public class XMLRead {
022            /** Once the XML file is read, it is stored in this object. */
023            private Document document;
024    
025            /** This is a class level variable that maintains the current record (node) 
026             number in use.  It is set by find()and used by getField(). 
027             */
028            private int intRecNum;
029    
030            /** The constructor only initializes intRecNum equal to a minus one. */
031            public XMLRead() {
032                    intRecNum = -1;
033            }
034    
035            /** 
036             This function accepts a URL to an XML page.  Once the page has  
037             been downloaded, it is parsed into a document object and  
038             intRecNum is set to zero so that the first record can be  
039             viewed. 
040             */
041            public void loadPage(String strURL) throws Exception {
042                    String strFileName = "";
043                    String strContent = "";
044                    Status("readXML: URL=" + strURL);
045    
046                    URL myWebAddress = new URL(strURL);
047                    Status("readXML: URL is set up");
048                    URLConnection myConnection = myWebAddress.openConnection();
049                    Status("readXML: Connection opened");
050                    InputStream myStream = myConnection.getInputStream();
051                    Status("readXML: Get URL done");
052    
053                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
054                    Status("readXML: Factory built");
055                    DocumentBuilder builder = factory.newDocumentBuilder();
056                    Status("readXML: Builder built");
057                    document = builder.parse(myStream);
058                    Status("readXML: Document is done");
059                    intRecNum = 0;
060            }
061    
062            /** 
063             This function is not used at this time. 
064             */
065            public String setTable(String strTable) throws Exception {
066                    NodeList list = document.getElementsByTagName(strTable);
067                    Node node = list.item(0);
068                    TransformerFactory tFactory = TransformerFactory.newInstance();
069                    Transformer transformer = tFactory.newTransformer();
070                    DOMSource source = new DOMSource(node);
071                    return source.getNode().getFirstChild().toString();
072            }
073    
074            /**  
075             This function will search for a value represented by strKey in  
076             a field indicated by strKeyField.   
077             <ul> 
078             <li>intRecNum is set to a minus one if it did not find the key</li> 
079             <li>intRecNum is set to the index number (position) if it did find the key. 
080             </ul> 
081             */
082            public int find(String strKeyField, String strKey) throws Exception {
083                    intRecNum = -1; // Class Level Variable 
084                    // create an array of nodes of strKeyFields 
085                    NodeList list = document.getElementsByTagName(strKeyField);
086                    // start searching at the beginning 
087                    int intCount = 0;
088                    // flag to stop the search once found 
089                    boolean blnContinue = true;
090                    while (intCount < list.getLength() && blnContinue) {
091                            // list.item contains an array of nodes made up of strKeyFields. 
092                            // getFirstChild() gets the first child node (which is the record itself) 
093                            // getNodeValue() gets the text contents of the field 
094                            if (list.item(intCount).getFirstChild().getNodeValue().equals(
095                                            strKey)) {
096                                    intRecNum = intCount;
097                                    blnContinue = false;
098                            }
099                            //Status(list.item(intCount).getFirstChild().getNodeValue()); 
100                            //Status("intCount= " + intCount); 
101                            intCount++;
102                    }
103                    return intRecNum;
104            }
105    
106            /** 
107             This method extracts the contents of the field at the 
108             the current intRecNum position. 
109             */
110            public String getField(String strField) throws Exception {
111                    String strRet = "";
112                    NodeList list = document.getElementsByTagName(strField);
113                    if (intRecNum > -1) {
114                            strRet = list.item(intRecNum).getFirstChild().getNodeValue().trim();
115                    }
116                    return strRet;
117            }
118    
119            /**  
120             This method extracts the contents of the field at the 
121             index pointed to by intRec.  This method changes the value 
122             of the class variable intRecNum. 
123             */
124            public String getField(String strField, int intRec) throws Exception {
125                    intRecNum = intRec; // Class Variable 
126                    return getField(strField);
127            }
128    
129            /** 
130             This method is just used to display data during testing. 
131             */
132            private void Status(String strVar) {
133                    System.out.println("XMLLibrary: " + strVar);
134            }
135    }