我需要在我的项目中提供帮助,我有一个Web服务,它会返回这样的XML响应:
<ads>
<id>6</id>
<lnk>http://www.nkahtani.com/</lnk>
</ads>
我使用SAXParser来解析元素,这是我的代码
AdvParser.java
public class AdvParser extends DefaultHandler{
String elementValue = null;
Boolean elementOn = false;
public static AdvList sitesList = null;
public static AdvList getSitesList() {
return sitesList;
}
public static void setSitesList(AdvList sitesList) {
AdvParser.sitesList = sitesList;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
elementOn=true;
if (localName.equals("ads"))
{
sitesList = new AdvList();
} else if (localName.equals("id")) {
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
elementOn = false;
/**
* Sets the values after retrieving the values from the XML tags
* */
if (localName.equalsIgnoreCase("id"))
sitesList.setId(elementValue);
else if (localName.equalsIgnoreCase("lnk"))
sitesList.setLink(elementValue);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
if (elementOn) {
elementValue = new String(ch, start, length);
Log.i("XMLParser", elementValue);
elementOn = false;
}
}
}
AdvList.java
public class AdvList {
private ArrayList<String> link = new ArrayList<String>();
private ArrayList<String> id = new ArrayList<String>();
public ArrayList<String> getLink() {
return link;
}
public void setLink( String link) {
this.link .add(link);
}
public ArrayList<String> getId() {
return id;
}
public void setId(String id) {
this.id .add(id);
}
}
这将调用网址
public class RegisterTask extends AsyncTask<Void, Integer, String> {
ProgressDialog dialog;
protected String doInBackground(Void... N) {
publishProgress(0);
try {
//Result = LoginRequest();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader=saxParser.getXMLReader();
AdvParser handler = new AdvParser();
xmlReader.setContentHandler(handler);
URL url = new URL("http://82.192.79.39:9000/api/ads/get");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
is = connection.getInputStream();
xmlReader.parse(new InputSource(is));
//saxParser.parse(is, handler);
//Log.i("Pars", handler.getNameAtt() +" "+handler.getNameAtt());
// TODO Auto-generated catch block
return "hi";
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
@Override
protected void onProgressUpdate(Integer... progress) {
if(progress[0] == 0)
{
dialog = ProgressDialog.show(SplashScreen.this, "Please wait",
"Login ,Please wait.", true);
super.onProgressUpdate(progress);
}
}
protected void onPostExecute(InputStream result) {
dialog.dismiss();
}
}
我的代码中没有任何异常,但问题是我没有得到任何结果,解析器只解析第一个标签(广告)而没有读取其他标签(id,lnk) 任何人都可以告诉我如何解决它
谢谢
答案 0 :(得分:0)
一段时间后我遇到了类似的问题,对我有用的是我为所有标签使用了不同的布尔值,并为所有标签值使用了不同的StringBuilder。以下是对处理程序的一些小修改。
public static final String ID = "id";
public static final String LINK = "lnk";
Boolean bool_id, bool_link,flag;
StringBuilder out_id,out_link;
Boolean elementOn = false;
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("ads")) {
flag = true;
}
if (flag) {
if (localName.equalsIgnoreCase(ID)) {
out_id= new StringBuilder();
elementOn = true;
bool_id= true;
} else if (localName.equalsIgnoreCase(LINK)) {
out_link = new StringBuilder();
elementOn = true;
bool_link = true;
}
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if (elementOn) {
if (bool_id)
out_id.append(new String(ch, start, start + length));
else if (bool_link)
out_link.append(new String(ch, start, start + length));
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (elementOn) {
elementOn = false;
if (localName.equalsIgnoreCase(ID)) {
sitesList.setId(out_id.toString());
bool_id = false;
} else if (localName.equalsIgnoreCase(LINK)) {
sitesList.setLink(out_link.toString());
bool_link= false;
}
}
}
希望这有帮助。