Reading XML Files


The XMLReader.java file reads the airlines.xml file that contains the information about available airline schedules. Listing 6-7 shows the contents of the XMLReader.java file:

Listing 6-7: The XMLReader.java File

start example
 /*Imports required File classes*/ import java.io.File; /*Imports required IOException classes*/ import java.io.IOException; /*Imports required PrintStream classes*/ import java.io.PrintStream; /*Imports required Iterator classes*/ import java.util.Iterator; /*Imports required List classes*/ import java.util.List; /*Imports required Document classes*/ import org.jdom.Document; /*Imports required Element classes*/ import org.jdom.Element; /*Imports required JDOMException classes*/ import org.jdom.JDOMException; /*Imports required SAXBuilder classes*/ import org.jdom.input.SAXBuilder; /*Imports required XMLOutputter classes*/ import org.jdom.output.XMLOutputter; /* class XMLReader -This class is used to read XML file. Methods: readFile */ public class XMLReader  { /*Declare object of String class.*/ String filename; /*Declare object of Document class.*/ Document doc; /* readFile: This method call an openPort() method. Parameter: fname - object of String class. Return Value: List */ public List readFile(String fname)   {    filename = fname;    PrintStream out = System.out;    SAXBuilder builder = new SAXBuilder();    try    {       doc = builder.build(new File(filename));    }    catch(Exception je) {}    Element root = doc.getRootElement();    List childlist = root.getChildren("flightdescription");    return childlist;    } } 
end example

Download this listing.

In the above listing, the XMLReader.java file contains the readFile() method. The readFile() method takes the location of the airlines.xml file as an input parameter and reads the file.




Developing Applications Using Jabber
Developing Applications Using Jabber
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 68

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net