Reputation: 14697
I am parsing a XML using SAXParser and trying to fetch the value for a specific node but my code is returning null
. I don't know what am I doing wrong. here is my code. Any help would be greatly appreciated.
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXParserUsage extends DefaultHandler {
SAXParserFactory factory;
SAXParser parser;
DefaultHandler handler;
private String elementName;
private String authorizationCode;
public String getAuthCodeResponse(String message) throws SAXException, IOException, ParserConfigurationException {
factory = SAXParserFactory.newInstance();
handler = new SAXParserUsage();
parser = factory.newSAXParser();
parser.parse(new InputSource(new StringReader(message)), handler);
return authorizationCode;
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
elementName = qName;
}
public void characters(char[] text, int start, int length) throws SAXException {
if (elementName.equals("code")) {
String code = new String(text, start, length);
System.out.println("setting code: " + code);
authorizationCode = code;
}
}
public void endElement(String arg0, String arg1, String arg2) throws SAXException {
}
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
SAXParserUsage usage = new SAXParserUsage();
String code = usage.getAuthCodeResponse(getSampleString());
System.out.println("code is: " + code);
}
private static String getSampleString() {
String sample = "<washCode><getCode><code>12345</code></getCode></washCode>";
return sample;
}
}
Upvotes: 0
Views: 798
Reputation: 163262
Note also that you can't rely on the content of an element being passed across to the characters() method in one piece. It can be split across multiple calls of the characters() method. This won't happen very often - typically only if the text contains entity references or if it crosses the boundary between two I/O buffers - but the fact that it's rare makes this bug very insidious. You need to buffer the content over multiple calls, and save it when you hit the endElement() event.
There's another technical bug in your code which is that the parser isn't obliged to supply the qName argument to startElement() if the http://xml.org/sax/features/namespace-prefixes property of the parser takes its default value of "false". However, I've never come across a parser which fails to supply this argument, so this one is unlikely to hit you in practice.
Upvotes: 1
Reputation: 16059
public String getAuthCodeResponse(String message) throws SAXException, IOException, ParserConfigurationException {
factory = SAXParserFactory.newInstance();
handler = new SAXParserUsage(); // <--- You create local instance here
parser = factory.newSAXParser();
parser.parse(new InputSource(new StringReader(message)), handler);
// ^^ parser writes to local instance here but ...
return authorizationCode;
// you return authCode field of the instance that this method is invoked on,
// which has not been changed by this method.
// So if it was null before, it still is null.
}
So you either have to return handler.authorizationCode
or parser.parse(..., this);
Upvotes: 1
Reputation: 9162
Try returning
handler.authorizationCode;
instead of
return authorizationCode;
Upvotes: 0