/********************************************************** * Newspaper accepts an author name and prints out the article title. * Kelli Wiseth * 24-October-2004 * Mid-term Exam * * XML and Web Services class | NDNU * *********************************************************** */ // Java core packages import java.io.*; // Java standard extensions import javax.xml.parsers.*; import javax.xml.transform.*; // third-party libraries import org.w3c.dom.*; import org.w3c.dom.traversal.*; import org.xml.sax.*; public class Newspaper { String author; String filename = "newspaper.xml"; String nodeValue = " "; boolean authorExists = false; DocumentBuilder newspaperParser; Document newspaper; Element root; Node nodeName; public Newspaper ( String authorFirst, String authorLast ) { String authorLookup = authorFirst + " " + authorLast; // parse XML, create DOM tree, and find the author try { // obtain default parser DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating( false ); DocumentBuilder newspaperParser = dbf.newDocumentBuilder(); Document newspaper = newspaperParser.parse( new File( filename ) ); newspaper.getDocumentElement().normalize(); NodeList articles = newspaper.getElementsByTagName("ARTICLE"); NodeList headlines = newspaper.getElementsByTagName("HEADLINE"); for (int x = 0; x < articles.getLength(); x++) { Element articleElement = (Element)articles.item(x); String authorElement = articleElement.getAttribute("AUTHOR"); Element headlineElement = (Element)headlines.item(x); if (authorElement.equalsIgnoreCase(authorLookup)) { authorExists=true; Node headlineText = headlineElement.getFirstChild(); System.out.println("The author " + authorLookup + " wrote \"" + headlineText.getNodeValue() + ".\""); } } if (!authorExists) System.out.println("No articles for author named " + authorLookup + " found in the newspaper."); } //close try block /* The parser exceptions aren't needed, since we're not creating a * DOM structure except by passing in a pre-validated XML file, and * we've turned off validation to validate against, raise any of * these exceptions in this program. The IOException is raised any * time the filename is incorrect. */ // handle exception thrown by DocumentBuilder catch ( ParserConfigurationException parserException ) { parserException.printStackTrace(); } // handle exception thrown by Parser catch ( SAXException saxException ) { saxException.printStackTrace(); } // handle exception thrown when reading data from file catch ( IOException ioException ) { System.out.println(); System.out.println("Try entering the adddress book filename again. "); System.out.println("There's something wrong with the name you entered."); System.exit( 1 ); } } //close constructor public static void main( String args[] ) { if ( args.length < 2 ) { System.out.print("Syntax: java Newspaper \n"); System.out.print("Please enter author's first and last name, as in: \n\n"); System.out.print(" java Newspaper herb caen \n"); System.exit( 1 ); } Newspaper report = new Newspaper ( args[0], args[1] ); }//close main method } //close class Newspaper