我想下面的模式來分析數據解析問題SAX解析器的Android
我的代碼是下面
public class PriceXMLParsingExample extends Activity {
/** Create Object For SiteList Class */
PriceList sitesList = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView name[];
TextView website[];
TextView category[];
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL("http://www.xmlcharts.com/cache/precious-metals.php");
/** Create handler to handle XML Tags (extends DefaultHandler) */
PriceXMLHandler myXMLHandler = new PriceXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = PriceXMLHandler.sitesList;
website = new TextView[sitesList.getWebsite().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getWebsite().size(); i++) {
website[i] = new TextView(this);
website[i].setText("Name = "+sitesList.getWebsite().get(i));
layout.addView(website[i]);
}
/** Set the layout view to display */
setContentView(layout);
}
}
PriceXMLHandler.java
public class PriceXMLHandler extends DefaultHandler {
Boolean currentElement = false;
String currentValue = null;
public static PriceList sitesList = null;
public static PriceList getSitesList() {
return sitesList;
}
public static void setSitesList(PriceList sitesList) {
PriceXMLHandler.sitesList = sitesList;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("prices"))
{
/** Start */
sitesList = new PriceList();
} else if (localName.equals("currency")) {
/** Get attribute value */
String attr = attributes.getValue("access");
sitesList.setCategory(attr);
}
else if (localName.equals("price")) {
String attr = attributes.getValue("access");
sitesList.setCategory(attr);
}
}
/** Called when tag closing (ex:- <name>AndroidPeople</name>
* -- </name>)*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
/** set value */
if (localName.equalsIgnoreCase("price"))
sitesList.setName(currentValue);
else if (localName.equalsIgnoreCase("currency"))
sitesList.setWebsite(currentValue);
}
/** Called to get tag characters (ex:- <name>AndroidPeople</name>
* -- to get AndroidPeople Character) */
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
小學ceList.java
public class PriceList {
/** Variables */
private ArrayList<String> currency = new ArrayList<String>();
private ArrayList<String> price = new ArrayList<String>();
private ArrayList<String> access = new ArrayList<String>();
/** In Setter method default it will return arraylist
* change that to add */
public ArrayList<String> getName() {
return currency;
}
public void setName(String name) {
this.currency.add(name);
}
public ArrayList<String> getWebsite() {
return price;
}
public void setWebsite(String website) {
this.price.add(website);
}
public ArrayList<String> getCategory() {
return access;
}
public void setCategory(String category) {
this.access.add(category);
}
}
當我運行上面的代碼我得到的只有銀價沒有整個圖案所以我怎麼能解決它的主意?
你有任何答案嗎?這完成了嗎? – Naren
@Naren:還是不是 –
你有沒有試過下面的解決方案...如果它適用於你接受它作爲+答案 – Naren