gpt4 book ai didi

java - android中的SAX解析

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

我有如下所示的 xml 文件:

 <ns:retrieveLastResponse>
<ns:return xsi:type="ax21:MinusEntry">
<ax21:entrydate>2010-07-02T17:29:35.492+02:00</ax21:entrydate>
<ax21:minus>true</ax21:minus>
<ax21:password>SECRET</ax21:password>
<ax21:text>Some text</ax21:text>
<ax21:username>John Doe</ax21:username>
</ns:return>
</ns:retrieveLastResponse>

我正在尝试像这样用 sax 解析它:

try {

URL rssUrl = new URL("ADDRESS OF WS THAT RETURNS XML FILE");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
//InputSource myInputSource = new InputSource(rssUrl.openConnection().getInputStream());
myXMLReader.parse(myInputSource);

//result.setText(myRSSHandler.getMessages().get(0).getDesc());
result.setText(Integer.toString(myRSSHandler.getMessages().size()));

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result.setText("Cannot connect RSS!");
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result.setText("Cannot connect RSS!");
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result.setText("Cannot connect RSS!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result.setText("Cannot connect RSS!");
}


}

private class RSSHandler extends DefaultHandler
{

private List<Message> messages;
private Message currentMessage;
private StringBuilder builder;


public List<Message> getMessages(){
return this.messages;
}

@Override
public void startDocument() throws SAXException {

messages = new ArrayList<Message>();
builder = new StringBuilder();
}

@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub

}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {

super.startElement(uri, localName, qName, attributes);

// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("ns:return")){
Log.d("XML","Zacetek xmla");
this.currentMessage = new Message();
}

}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {

super.endElement(uri, localName, qName);

if (this.currentMessage != null){
if (localName.equalsIgnoreCase("ax21:entrydate")){
Log.d("XML","Vstop v datum");
currentMessage.setDate(builder.toString());

} else if (localName.equalsIgnoreCase("ax21:minus")){
currentMessage.setMin(builder.toString());
} else if (localName.equalsIgnoreCase("ax21:text")){
currentMessage.setDesc(builder.toString());
} else if (localName.equalsIgnoreCase("ax21:username")){
currentMessage.setUser(builder.toString());
}
else if(localName.equalsIgnoreCase("ns:return")){
messages.add(currentMessage);
}
builder.setLength(0);
}

}



@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);

builder.append(ch, start, length);
}

但是我的消息列表总是空的,endElement 和 startElement 中的日志消息永远不会显示。我如何检查程序是否完全获取了 xml 文件,或者我是否错误地解析了它?

最佳答案

问题出在 endElement 和 startElement if 子句中。我应该检查一下 "if (localName.equalsIgnoreCase("minus"))"而不是 "if (localName.equalsIgnoreCase("ax21:minus"))"

我还按照建议添加了这一行:mySAXParserFactory.setNamespaceAware(true);

关于java - android中的SAX解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3201918/

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