gpt4 book ai didi

java - NullPointerException Java SAXParser

转载 作者:行者123 更新时间:2023-11-30 04:11:53 25 4
gpt4 key购买 nike

我正在使用 SAX 解析 XML。我写的代码就像我在互联网上看到的例子一样。我得到了 url,然后我解析了它。这是我的代码。

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();
}
}

然后我编写了一个用于解析的处理程序。如下;

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());
}
}

然后我在 Main 方法中使用了这样的;

     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();
}

我看不到错误。但它给出了 NullPointerException 错误。这是 url =“http://www.gold.com.tr/cok-satanlar-rss/54109/0

最佳答案

您用来学习 SAX 的来源是不好的例子,请参阅 the Oracle tutorial如果您想知道如何以正确的方式执行此操作(或查看 the accepted answer to your previous question 上的评论,这也指出了一个很好的教程)。

但是 SAX 对于您想要的东西来说可能有点过分了,XPath 可能是一个更简单的解决方案。它更具声明性并且陷阱更少。

下面是一个示例 XML 文件,名为 foobar.xml:

<?xml version="1.0"?>
<foo>
<bar>hello</bar>
</foo>

我可以使用 Java SDK 中的 XPath API 来检索 bar 的元素文本,如下所示:

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);

字段文本将包含“hello”。

关于java - NullPointerException Java SAXParser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19405710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com