我想按照模式解析数据
我的代码如下
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;
}
}
}
PriceList.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);
}
}
当我运行上面的代码时,我只得到银价而不是整个模式,那么我该如何解决它呢?