我一直在尝试从特定 URL 读取 XML 数据。我得到了一些关于使用 SAX 和 DOM 提取 XML 数据的材料,但它不适用于我的特定 URL
这是我尝试过的代码。这个使用 SAX 并与
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.androidpeople.com/wp-content/uploads/2010/06/example.xml");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
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 = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
category = new TextView[sitesList.getName().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));
category[i] = new TextView(this);
category[i].setText("Website Category = "+sitesList.getCategory().get(i));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
代码中还包含扩展 DefaultHandler 的 XMLHandler 类
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("maintag"))
{
/** Start */
sitesList = new SitesList();
} else if (localName.equals("website")) {
/** Get attribute value */
String attr = attributes.getValue("category");
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("name"))
sitesList.setName(currentValue);
else if (localName.equalsIgnoreCase("website"))
sitesList.setWebsite(currentValue);
}
但是当我更改 url 时,它返回一个 XML Pasing Exception = java.net.UnknownHostException: Giantstrides.crs.gov.ng 和一个无法启动活动错误(使用 Tomcat)
TextView title[];
TextView description[];
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
URL sourceUrl = new URL(
"http://giantstrides.crs.gov.ng/index.php?option=com_ninjarsssyndicator&feed_id=22&format=raw");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
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 = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
title = new TextView[sitesList.getTitle().size()];
description = new TextView[sitesList.getTitle().size()];
//category = new TextView[sitesList.getName().size()];
/** Set the result text in textview and add it to layout */
//for (int i = 0; i < sitesList.getTitle().size(); i++) {
for (int i = 0; i < 2; i++) {
title[i] = new TextView(this);
title[i].setText("Title = "+sitesList.getTitle().get(i));
description[i] = new TextView(this);
description[i].setText("Description = "+sitesList.getDescription().get(i));
//category[i] = new TextView(this);
//category[i].setText("Website Category = "+sitesList.getCategory().get(i));
layout.addView(title[i]);
layout.addView(description[i]);
URL 没有问题,因为它会在浏览器上加载提要,但我无法获得所需的信息。拜托,我需要帮助,任何人,请..
戈齐