问题
I am parsing an XML with SAX. I wrote the codes like on tje internet examples that I have seen.I get url and then i parsed this. Here is my codes.
public class Urunler implements Serializable {
private String title;
private String description;
public Urunler(String title, String description) {
this.title = title;
this.description = description;
}
public Urunler() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String cevir() {
StringBuffer sb = new StringBuffer();
sb.append("Title:" + getTitle());
sb.append(", ");
sb.append("Description:" + getDescription());
sb.append(".");
return sb.toString();
}
}
Then I have written a handler for parsing. It is below;
public class UrunlerHandler extends DefaultHandler{
private Urunler urunler;
private String temp;
private ArrayList<Urunler> urunlerList= new ArrayList<Urunler>();
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
temp = "";
if(qName.equalsIgnoreCase("item")){
urunler = new Urunler();
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
temp = new String(ch,start,length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equalsIgnoreCase("item")){
urunlerList.add(urunler);
}else if(qName.equalsIgnoreCase("title")){
urunler.setTitle(temp);
}else if(qName.equalsIgnoreCase("description")) {
urunler.setDescription(temp);
}
}
public void readList(){
Iterator<Urunler> it = urunlerList.iterator();
while (it.hasNext()) {
System.out.println(it.next().cevir());
}
}
Then I have used in Main method like this;
try {
SAXParserFactory spfac = SAXParserFactory.newInstance();
SAXParser sp = spfac.newSAXParser();
UrunlerHandler handler = new UrunlerHandler();
URL geoLocationDetailXMLURL = new URL(url);
URLConnection geoLocationDetailXMLURLConnection = geoLocationDetailXMLURL.openConnection();
BufferedReader geoLeocationDetails = new BufferedReader(new InputStreamReader(geoLocationDetailXMLURLConnection.getInputStream(), "UTF-8"));
InputSource inputSource = new InputSource(geoLeocationDetails);
sp.parse(inputSource, handler);
handler.readList();
} catch (Exception e) {
e.printStackTrace();
}
I cant't see en error. But it is giving error that NullPointerException. Here is the url = "http://www.gold.com.tr/cok-satanlar-rss/54109/0"
回答1:
The sources you're using to learn SAX from are bad examples, see the Oracle tutorial if you want to know how to do this the right way (or see the comment on the accepted answer to your previous question, which also points to a good tutorial).
But SAX is probably overkill for what you want here, XPath may be a simpler solution. It is more declarative and has less pitfalls.
Here's an example XML file, called foobar.xml:
<?xml version="1.0"?>
<foo>
<bar>hello</bar>
</foo>
I can use the XPath API that is part of the Java SDK to retrieve the element text for bar like this:
org.xml.sax.InputSource inputSource = new
org.xml.sax.InputSource('c:/users/ndh/foobar.xml');
javax.xml.xpath.XPath xpath =
javax.xml.xpath.XPathFactory.newInstance().newXPath();
String text = (String)xpath.evaluate('/foo/bar/text()',
inputSource, javax.xml.xpath.XPathConstants.STRING);
The field text will contain "hello".
回答2:
Since urunler is null in your handler class, you should either be doing (A) a null check if it is allowed to be null or (B) defining what urunler is in the class constructor so that it can't be null:
(A)
// ...
if (urunler != null) {
urunler.setTitle("foo");
}
(B)
private final Urunler urunler;
public UrunlerHanlder(String qName) {
urunler = new Urunler();
}
回答3:
Your problem is that you're encountering the <title>
element under the <channel>
element before you've encountered an <item>
element. Because of that, your urunler
is still null when you try to call the setTitle
method on it.
What you should probably do is to check that urunler
is not null before you try to set that value (as Rogue suggests), since apparently there are <title>
elements that you're not actually interested in.
来源:https://stackoverflow.com/questions/19405710/nullpointerexception-java-saxparser