Results 1 to 3 of 3
  1. #1
    Laffel is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default [Solved] XML and SAX - class returns 'null'

    Hello forum,
    I'm currently struggling with parsing an XML-document and store the data properly while parsing. The scenario goes like this

    something.xml:
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <data>
    	<phrase id="1" value="something"/>
    	<phrase id="2" value="different"/>
    </data>
    Parser.java:
    Java Code:
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    
    public class Parser extends DefaultHandler{
    	
    	private String id, value, something, different;
    	// private String xmlfile = "something.xml";
    	int index;
    	
    	public Parser(){
    	}
    	
    	public void parseDocument(){
    		try {
    			SAXParserFactory factory = SAXParserFactory.newInstance();
    			SAXParser parser = factory.newSAXParser();
    			DefaultHandler handler = new Parser();
    			parser.parse("something.xml", handler);
    		}
    		catch (Exception e) {
    		}
    	}
    	// start element
    	public void startElement(String uri, String localName, String qname, Attributes atts) throws SAXException {
    		if(qname.equals("phrase")){
    			id = atts.getValue("id");
    			value = atts.getValue("value");
    		}
    		index = Integer.valueOf(id);
    	}
    	// end element
    	public void endElement(String uri, String localName, String qname) throws SAXException{
    		doStuff(index, value);
    	}
    	// switch case manipulation
    	public void doStuff(int i, String s){
    
    		switch(i){
    		case 1: setSomething(s); break;
    		case 2: setDifferent(s); break;
    		default: System.out.println("Oh noes!");
    		
    		}
    	}
    	
    	public void setSomething(String s){
    		something = s;
    	}
    	
    	public void setDifferent(String s){
    		different = s;
    	}
    	
    	public String getSomething(){
    		return something;
    	}
    	
    	public String getDifferent(){
    		return different;
    	}
    }
    and the testclass, ParserTest.java
    Java Code:
    public class ParserTest {
    
    	public static void main(String[] args) {
    		Parser p = new Parser();
    		p.parseDocument();
    		
    		System.out.printf("%s %s", p.getSomething(), p.getDifferent());
    	}
    }
    while I expect the output to be "something different" i get "null null". If I print the data while being extracted / parsed I get the correct values...

    Any ideas?

    // Laffel
    Last edited by Laffel; 03-07-2011 at 09:22 PM. Reason: issue solved

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Ok, crazy code :)

    First: in parseDociument you are creating a new parser object, that is not the same as your parser object in your main method so you never use that to parse/ it will never set the variables of that object
    --> change
    Java Code:
    			DefaultHandler handler = new Parser();
    			parser.parse("something.xml", handler);
    to
    -->
    Java Code:
    			// DELETE THIS LINE !!! // DefaultHandler handler = new Parser();
    			parser.parse("something.xml", this); // use this !
    Second: catch (Exception e) {}
    that is ugly and you will never see if you get exceptions (and here: you gets an exception on index = Integer.valueOf(id); because it stands outside of your if statement ?!)

    Third: move index = Integer.valueOf(id); into your if statement

    Java Code:
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    
    public class Parser extends DefaultHandler{
    	
    	private String id, value, something, different;
    	// private String xmlfile = "something.xml";
    	int index;
    	
    	public void parseDocument(){
    		try {
    			SAXParserFactory factory = SAXParserFactory.newInstance();
    			SAXParser parser = factory.newSAXParser();
    			parser.parse("something.xml", this);
    		}
    		catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	// start element
    	public void startElement(String uri, String localName, String qname, Attributes atts) throws SAXException {
    		if(qname.equals("phrase")){
    			id = atts.getValue("id");
    			value = atts.getValue("value");
    			index = Integer.valueOf(id);
    		}
    	}
    	// end element
    	public void endElement(String uri, String localName, String qname) throws SAXException{
    		doStuff(index, value);
    	}
    	// switch case manipulation
    	public void doStuff(int i, String s){
    
    		switch(i){
    		case 1: setSomething(s); break;
    		case 2: setDifferent(s); break;
    		default: System.out.println("Oh noes!");
    		
    		}
    	}
    	
    	public void setSomething(String s){
    		something = s;
    	}
    	
    	public void setDifferent(String s){
    		different = s;
    	}
    	
    	public String getSomething(){
    		return something;
    	}
    	
    	public String getDifferent(){
    		return different;
    	}
    }

    btw: very complicated way to parse your simple xml file :D

  3. #3
    Laffel is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    OMFG! I can't belive that "small" change did the difference... And I can't believe I didn't see it earlier :) I've been though this code for almost two whole days, ripping my hair out...

    And yes, this is just a resampled code of what I'm gonna use it for (which is why I dont catch exceptions - yet :D )

    Again, thanx for saving my life

Similar Threads

  1. Splashscreen returns null
    By Charlie161 in forum AWT / Swing
    Replies: 2
    Last Post: 03-04-2011, 01:25 PM
  2. Replies: 1
    Last Post: 02-07-2011, 10:44 AM
  3. getImplementationVersion() returns null
    By newbiejava in forum New To Java
    Replies: 22
    Last Post: 09-12-2010, 09:31 AM
  4. Replies: 1
    Last Post: 08-20-2010, 11:30 AM
  5. helpset findHelpSet returns null
    By kmm1977 in forum AWT / Swing
    Replies: 23
    Last Post: 06-22-2010, 03:53 PM

Tags for this Thread

Posting Permissions

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