Results 1 to 3 of 3
- 03-07-2011, 08:19 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
[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:
Parser.java:Java Code:<?xml version="1.0" encoding="UTF-8"?> <data> <phrase id="1" value="something"/> <phrase id="2" value="different"/> </data>
and the testclass, ParserTest.javaJava 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; } }
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...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()); } }
Any ideas?
// LaffelLast edited by Laffel; 03-07-2011 at 09:22 PM. Reason: issue solved
- 03-07-2011, 09:06 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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
toJava Code:DefaultHandler handler = new Parser(); parser.parse("something.xml", handler);
-->
Second: catch (Exception e) {}Java Code:// DELETE THIS LINE !!! // DefaultHandler handler = new Parser(); parser.parse("something.xml", this); // use this !
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
- 03-07-2011, 09:14 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
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
-
Splashscreen returns null
By Charlie161 in forum AWT / SwingReplies: 2Last Post: 03-04-2011, 01:25 PM -
String passed as argument to a constructor returns null value
By eLancaster in forum New To JavaReplies: 1Last Post: 02-07-2011, 10:44 AM -
getImplementationVersion() returns null
By newbiejava in forum New To JavaReplies: 22Last Post: 09-12-2010, 09:31 AM -
Lucene highlighter returns null fragments always
By zee in forum LuceneReplies: 1Last Post: 08-20-2010, 11:30 AM -
helpset findHelpSet returns null
By kmm1977 in forum AWT / SwingReplies: 23Last Post: 06-22-2010, 03:53 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks