Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: xml & java

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Im having trouble editing a java program so that it prints the presidents first name and last name and was wondering if anyone could give me some pointers

    xml

     
    <presidents>
    	<president>
    		<term from="1889" to="1893"/>
    		<name>
    			<pres_first>Benjamin</pres_first>
    			<pres_last>Harrison</pres_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>
    			<pres_first>Stephen</pres_first>
    			<pres_middle>Grover</pres_middle>
    			<pres_last>Cleveland</pres_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>
    			<pres_first>William</pres_first>
    			<pres_last>McKinley</pres_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>
    			<pres_first>Theodore</pres_first>
    			<pres_last>Roosevelt</pres_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>
    			<pres_first>William</pres_first>
    			<pres_middle>Howard</pres_middle>
    			<pres_last>Taft</pres_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>
    			<pres_first>Thomas</pres_first>
    			<pres_middle>Woodrow</pres_middle>
    			<pres_last>Wilson</pres_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>
    			<pres_first>Warren</pres_first>
    			<pres_middle>Gamaliel</pres_middle>
    			<pres_last>Harding</pres_last>
    		</name>
    		<party>Republican</party>
    		<vicePresident>
    			<name>
    				<first>Calvin</first>
    				<last>Coolidge</last>
    			</name>
    		</vicePresident>
    	</president>
    	<president>
    		<term from="1923" to="1929"/>
    		<name>
    			<pres_first>John</pres_first>
    			<pres_middle>Calvin</pres_middle>
    			<pres_last>Coolidge</pres_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>
    			<pres_first>Herbert</pres_first>
    			<pres_middle>Clark</pres_middle>
    			<pres_last>Hoover</pres_last>
    		</name>
    		<party>Republican</party>
    		<vicePresident>
    			<name>
    				<first>Charles</first>
    				<last>Curtis</last>
    			</name>
    		</vicePresident>
    	</president>
    	<president>
    		<term from="1933" to="1945"/>
    		<name>
    			<pres_first>Franklin</pres_first>
    			<pres_middle>Delano</pres_middle>
    			<pres_last>Roosevelt</pres_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>
    			<pres_first>Harry</pres_first>
    			<pres_middle>S</pres_middle>
    			<pres_last>Truman</pres_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>
    			<pres_first>Dwight</pres_first>
    			<pres_middle>David</pres_middle>
    			<pres_last>Eisenhower</pres_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>
    			<pres_first>John</pres_first>
    			<pres_middle>Fitzgerald</pres_middle>
    			<pres_last>Kennedy</pres_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>
    			<pres_first>Lyndon</pres_first>
    			<pres_middle>Baines</pres_middle>
    			<pres_last>Johnson</pres_last>
    		</name>
    		<party>Democratic</party>
    		<vicePresident>
    			<name>
    				<first>Hubert</first>
    				<last>Humphrey</last>
    			</name>
    		</vicePresident>
    	</president>
    	<president>
    		<term from="1969" to="1974"/>
    		<name>
    			<pres_first>Richard</pres_first>
    			<pres_middle>Milhous</pres_middle>
    			<pres_last>Nixon</pres_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>
    			<pres_first>Gerald</pres_first>
    			<pres_middle>Rudolph</pres_middle>
    			<pres_last>Ford</pres_last>
    		</name>
    		<party>Republican</party>
    		<vicePresident>
    			<name>
    				<first>Nelson</first>
    				<last>Rockefeller</last>
    			</name>
    		</vicePresident>
    	</president>
    	<president>
    		<term from="1977" to="1981"/>
    		<name>
    			<pres_first>James</pres_first>
    			<pres_middle>Earl</pres_middle>
    			<pres_last>Carter</pres_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>
    			<pres_first>Ronald</pres_first>
    			<pres_middle>Wilson</pres_middle>
    			<pres_last>Reagan</pres_last>
    		</name>
    		<party>Republican</party>
    		<vicePresident>
    			<name>
    				<first>George</first>
    				<last>Bush</last>
    			</name>
    		</vicePresident>
    	</president>
    	<president>
    		<term from="1989" to="1993"/>
    		<name>
    			<pres_first>George</pres_first>
    			<pres_middle>Herbert</pres_middle>
    			<pres_middle>Walker</pres_middle>
    			<pres_last>Bush</pres_last>
    		</name>
    		<party>Republican</party>
    		<vicePresident>
    			<name>
    				<first>Dan</first>
    				<last>Quayle</last>
    			</name>
    		</vicePresident>
    	</president>
    	<president>
    		<term from="1993" to="2001"/>
    		<name>
    			<pres_first>William</pres_first>
    			<pres_middle>Jefferson</pres_middle>
    			<pres_last>Clinton</pres_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>
    			<pres_first>George</pres_first>
    			<pres_middle>W</pres_middle>
    			<pres_last>Bush</pres_last>
    		</name>
    		<party>Republican</party>
    		<vicePresident>
    			<name>
    				<first>Dick</first>
    				<last>Cheney</last>
    			</name>
    		</vicePresident>
    	</president>
     
    <president>
    		<term from="2009" to="present"/>
    		<name>
    			<pres_first>Barack</pres_first>
    			<pres_last>Obama</pres_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
    // http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPSAX3.html
    // 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 ModesProcessor {
     
    	public static void main (String argv []) {
     
    		String fileName = "modes3.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 use a stack to keep
    // track of the structure of 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);
    	}
     
    	public void characters (char buf [], int offset, int length)
    														throws SAXException {
    		if ("presidents".equals (stack.peek ())) {
    			String s = new String (buf, offset, length);
    			System.out.print (stack.peek() + " = " + s + "\n");
    		}
    		return;
    	}
     
    	public void endElement (String uri, String local, String qName)
    														throws SAXException {
    		stack.pop ();
      }
     
    }

    any help greatly appreciated

    does any one know how i could construct a for loop method so it returns the term dates in the following xml file;
    Last edited by helloworld922; February 16th, 2010 at 10:23 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: xml & java

    If you get a hold of JDOM you can do it like this.

    import java.io.File;
    import java.util.List;
     
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
     
    public class JdomTest1 {
        @SuppressWarnings("unchecked")
        public static void main(String[] args) throws Throwable {
            final SAXBuilder builder = new SAXBuilder();
            final Document document = builder.build(new File("modes3.xml"));
     
            final List<Element> presidents = document.getRootElement().getChildren("president");
     
            if (presidents != null && !presidents.isEmpty()) {
                for (final Element president : presidents) {
                    final String from = president.getChild("term").getAttributeValue("from");
                    final String to = president.getChild("term").getAttributeValue("to");
                    final Element nameElement = president.getChild("name");
     
                    if (nameElement != null) {
                        System.out.println("President [" + nameElement.getChildText("pres_first") + " " + nameElement.getChildText("pres_last") + "] served from [" + from + "] to [" + to + "]");
                    }
                }
            }
        }
    }

    // Json