特定の 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 と Unable to start activity エラー (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 はブラウザにフィードをロードするので問題ありませんが、必要な情報を取得できません。お願いします、助けが必要です、誰かお願いします..
ゴージー