当前位置: 代码迷 >> Android >> sax解析xml解决办法
  详细解决方案

sax解析xml解决办法

热度:119   发布时间:2016-05-01 22:03:23.0
sax解析xml
package com.example.android.apis;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RateHandler extends DefaultHandler {
private List<Rate> rates;
private Rate rate;
private String tagName;// 作用是记录解析时的上一个节点名称

public List<Rate> getRates() {
return rates;
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String data=new String(ch, start, length);
if (tagName.equals("Symbol")) {
rate.setSymbol(data);
}
if (tagName.equals("Name")) {
rate.setName(data);
}
if (tagName.equals("mBuyPrice")) {
rate.setmBuyPrice(data);
}
if (tagName.equals("fBuyPrice")) {
rate.setfBuyPrice(data);
}
if (tagName.equals("SellPrice")) {
rate.setSellPrice(data);
}
if(tagName.endsWith("BasePrice")&&data!=null){
rate.setBasePrice(data);
}

}


//public void startDocument() throws SAXException {
// TODO Auto-generated method stub
// rates = new ArrayList<Rate>();
//}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if ("ForexRmbRate".equals(localName)) {
rates.add(rate);

}

}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
tagName = localName;
if ("getForexRmbRate".equals(localName)) {
rates = new ArrayList<Rate>();
}
if ("ForexRmbRate".equals(localName)) {
rate = new Rate();
}
}

}

在解析这个http://webservice.webxml.com.cn/WebServices/ForexRmbRateWebService.asmx/getForexRmbRate xml文件时
为什么解析出来的
都是空的 DEBUG里面看rate里面的数据都是" "什么都没有
  何解 大神们求解啊 烦恼了我两天了 看到我吐血了

------解决方案--------------------
有时候查一下API,还好一些!
  相关解决方案