我正在用SAX解析XML。我把代码写在我看到的tje网络例子上,我得到了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());
}
}然后我用了这样的主要方法;
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“
发布于 2013-10-16 15:27:32
用于学习SAX的源代码是不好的例子,如果您想知道如何以正确的方式来学习SAX,请参阅Oracle教程 (或者查看对你上一个问题的公认答案上的注释,这也指向了一个好的教程)。
但是SAX可能对您想要的东西来说太过分了,XPath可能是一个更简单的解决方案。它具有更强的声明性和较少的陷阱。
下面是一个名为foobar.xml的示例XML文件:
<?xml version="1.0"?>
<foo>
<bar>hello</bar>
</foo>我可以使用Java的一部分XPath API检索条形图的元素文本,如下所示:
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“。
发布于 2013-10-16 14:16:57
由于urunler在您的处理程序类中是空的,您应该执行(A)空检查(如果允许它是空的),或者(B)在类构造函数中定义urunler的内容,以便它不能是空的:
(A)
// ...
if (urunler != null) {
urunler.setTitle("foo");
}(B)
private final Urunler urunler;
public UrunlerHanlder(String qName) {
urunler = new Urunler();
}发布于 2013-10-16 14:55:25
您的问题是,在遇到<title>元素之前,您在<channel>元素下遇到了<item>元素。正因为如此,当您尝试调用urunler上的setTitle方法时,它仍然是空的。
您可能应该做的是在尝试设置该值之前检查urunler是否为null (如Rogue所建议的),因为显然存在一些您并不感兴趣的<title>元素。
https://stackoverflow.com/questions/19405710
复制相似问题