Results 1 to 2 of 2

Thread: Java & XML

  1. #1
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default Java & XML

    my task is to edit an existing parser handling java program so that it prints out each US Presidents first nam, last name, and term dates and i'm not sure where to begin if anyone has any pointers it would be greatly appreciated;

    XML file:

    <presidents>
    <president>
    <term from="1889" to="1893"/>
    <name>
    <first>Benjamin</first>
    <last>Harrison</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>Levi</first>
    <middle>P</middle>
    <last>Morton</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1893" to="1897"/>
    <name>
    <first>Stephen</first>
    <middle>Grover</middle>
    <last>Cleveland</last>
    <nickname>Grover</nickname>
    </name>
    <party>Democratic</party>
    <vicePresident>
    <name>
    <first>Adlai</first>
    <middle>E</middle>
    <last>Stevenson</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1897" to="1901"/>
    <name>
    <first>William</first>
    <last>McKinley</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>Garret</first>
    <middle>A</middle>
    <last>Hobart</last>
    </name>
    </vicePresident>
    <vicePresident>
    <name>
    <first>Theodore</first>
    <last>Roosevelt</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1901" to="1909"/>
    <name>
    <first>Theodore</first>
    <last>Roosevelt</last>
    <nickname>Teddy</nickname>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>Charles</first>
    <middle>W</middle>
    <last>Fairbanks</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1909" to="1913"/>
    <name>
    <first>William</first>
    <middle>Howard</middle>
    <last>Taft</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>James</first>
    <middle>S</middle>
    <last>Sherman</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1913" to="1921"/>
    <name>
    <first>Thomas</first>
    <middle>Woodrow</middle>
    <last>Wilson</last>
    </name>
    <party>Democratic</party>
    <vicePresident>
    <name>
    <first>Thomas</first>
    <middle>R</middle>
    <last>Marshall</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1921" to="1923"/>
    <name>
    <first>Warren</first>
    <middle>Gamaliel</middle>
    <last>Harding</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>Calvin</first>
    <last>Coolidge</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1923" to="1929"/>
    <name>
    <first>John</first>
    <middle>Calvin</middle>
    <last>Coolidge</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>Charles</first>
    <middle>G</middle>
    <last>Dawes</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1929" to="1933"/>
    <name>
    <first>Herbert</first>
    <middle>Clark</middle>
    <last>Hoover</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>Charles</first>
    <last>Curtis</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1933" to="1945"/>
    <name>
    <first>Franklin</first>
    <middle>Delano</middle>
    <last>Roosevelt</last>
    </name>
    <party>Democratic</party>
    <vicePresident>
    <name>
    <first>John</first>
    <middle>N</middle>
    <last>Garner</last>
    </name>
    </vicePresident>
    <vicePresident>
    <name>
    <first>Henry</first>
    <middle>A</middle>
    <last>Wallace</last>
    </name>
    </vicePresident>
    <vicePresident>
    <name>
    <first>Harry</first>
    <middle>S</middle>
    <last>Truman</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1945" to="1953"/>
    <name>
    <first>Harry</first>
    <middle>S</middle>
    <last>Truman</last>
    </name>
    <party>Democratic</party>
    <vicePresident>
    <name>
    <first>Alben</first>
    <middle>W</middle>
    <last>Barkley</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1953" to="1961"/>
    <name>
    <first>Dwight</first>
    <middle>David</middle>
    <last>Eisenhower</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>Richard</first>
    <middle>M</middle>
    <last>Nixon</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1961" to="1963"/>
    <name>
    <first>John</first>
    <middle>Fitzgerald</middle>
    <last>Kennedy</last>
    <nickname>Jack</nickname>
    </name>
    <party>Democratic</party>
    <vicePresident>
    <name>
    <first>Lyndon</first>
    <middle>Baines</middle>
    <last>Johnson</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1963" to="1969"/>
    <name>
    <first>Lyndon</first>
    <middle>Baines</middle>
    <last>Johnson</last>
    </name>
    <party>Democratic</party>
    <vicePresident>
    <name>
    <first>Hubert</first>
    <last>Humphrey</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1969" to="1974"/>
    <name>
    <first>Richard</first>
    <middle>Milhous</middle>
    <last>Nixon</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>Spiro</first>
    <middle>T</middle>
    <last>Agnew</last>
    </name>
    </vicePresident>
    <vicePresident>
    <name>
    <first>Gerald</first>
    <middle>Rudolph</middle>
    <last>Ford</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1974" to="1977"/>
    <name>
    <first>Gerald</first>
    <middle>Rudolph</middle>
    <last>Ford</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>Nelson</first>
    <last>Rockefeller</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1977" to="1981"/>
    <name>
    <first>James</first>
    <middle>Earl</middle>
    <last>Carter</last>
    <nickname>Jimmy</nickname>
    </name>
    <party>Democratic</party>
    <vicePresident>
    <name>
    <first>Walter</first>
    <last>Mondale</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1981" to="1989"/>
    <name>
    <first>Ronald</first>
    <middle>Wilson</middle>
    <last>Reagan</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>George</first>
    <last>Bush</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1989" to="1993"/>
    <name>
    <first>George</first>
    <middle>Herbert</middle>
    <middle>Walker</middle>
    <last>Bush</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>Dan</first>
    <last>Quayle</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="1993" to="2001"/>
    <name>
    <first>William</first>
    <middle>Jefferson</middle>
    <last>Clinton</last>
    <nickname>Bill</nickname>
    </name>
    <party>Democratic</party>
    <vicePresident>
    <name>
    <first>Al</first>
    <last>Gore</last>
    </name>
    </vicePresident>
    </president>
    <president>
    <term from="2001" to="2008"/>
    <name>
    <first>George</first>
    <middle>W</middle>
    <last>Bush</last>
    </name>
    <party>Republican</party>
    <vicePresident>
    <name>
    <first>Dick</first>
    <last>Cheney</last>
    </name>
    </vicePresident>
    </president>

    <president>
    <term from="2009" to="present"/>
    <name>
    <first>Barack</first>
    <last>Obama</last>
    </name>
    <party>Democratic</party>
    <vicePresident>
    <name>
    <first>Joe</first>
    <last>Biden</last>
    </name>
    </vicePresident>
    </president>
    </presidents>








    java file:


    // Program based on Java tutorial at
    // Echoing an XML File with the SAX Parser
    // Modification for Java 6 based on
    // docs/technotes/guides/xml/jaxp/ReleaseNotes_160.html#feature

    import java.io.IOException;
    import org.xml.sax.*;
    import org.xml.sax.helpers.DefaultHandler;
    import javax.xml.parsers.SAXParserFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;

    import java.util.Stack;

    public class ParserHandler5 {

    public static void main (String argv []) {

    String fileName = "clocks.xml";

    SAXParser producer;
    DefaultHandler consumer;

    // Get an instance of the default XML parser class
    SAXParserFactory factory = SAXParserFactory.newInstance();

    try {
    producer = factory.newSAXParser();
    }
    catch (Throwable t) {
    System.err.println (
    "Can't get parser, check configuration: "
    + t.getMessage ());
    return;
    }

    // set up the consumer
    try {
    // Get a consumer for all the parser events
    consumer = new MyHandler ();
    }
    catch (Exception e) {
    // consumer setup can uncover errors,
    // though this simple one shouldn't
    System.err.println (
    "Can't set up consumers:"
    + e.getMessage ());
    return;
    }

    // Do the parse using consumer as the handler
    try {
    producer.parse (fileName, consumer);
    System.out.println("\nXML document in file " + fileName + " parsed OK");
    }
    catch (IOException e) {
    System.out.println ("I/O error:\n" + e.toString());
    }
    catch (SAXParseException e) {
    System.out.println ("Parsing error:\n" + e.toString ());
    }
    catch (SAXException e) {
    System.out.println(fileName + " unspecified SAX exception:\n"
    + e.toString());
    }
    }
    }

    // This class handles all call-backs from the parser
    // Methods startElement, endElement and characters
    // play a similar role to the method actionPerformed
    // in the interface Action Listener.

    // In this example these methods simply identify and
    // the value of each element in the XML document.

    class MyHandler extends DefaultHandler {

    private Stack<String> stack = new Stack<String> ();

    public MyHandler() {
    super();
    }

    public void startElement(String uri, String local, String qName,
    Attributes atts) throws SAXException {
    stack.push (qName);
    System.out.print(qName + ": ");
    }

    public void characters (char buf [], int offset, int length)
    throws SAXException {
    String s = new String (buf, offset, length);
    System.out.print(s);

    return;
    }

    public void endElement (String uri, String local, String qName)
    throws SAXException {
    stack.pop ();
    }

    }

  2. #2
    tsili is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •