似乎您没有按照我提供的那样更改代码。如果您坚持要这样解析,则需要先获取 xml 并对其进行操作以进行正确解析。我还在此消息末尾提供了一个类以获取 xml 作为文本。请像这样更改您的代码,尝试编写结果。
如果您更改此行,您将成功。
从 getStories 函数中删除此行:
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL u = new URL(address);
Document doc = builder.parse(u.openStream());
而不是那些删除的行,添加这些:
WebRequest response = new WebRequest("http://www.ncataggies.com/rss.dbml?db_oem_id=24500&RSS_SPORT_ID=74515&media=news",PostType.GET);
String htmltext = response.Get();
int firtItemIndex = htmltext.indexOf("<item>");
String htmltextHeader = htmltext.substring(0,firtItemIndex);
String htmltextBody = htmltext.substring(firtItemIndex);
htmltextBody = htmltextBody.replace("<title>", "<title><![CDATA[ ");
htmltextBody = htmltextBody.replace("</title>", "]]></title>");
htmltextBody = htmltextBody.replace("<link>", "<link><![CDATA[ ");
htmltextBody = htmltextBody.replace("</link>", "]]></link>");
htmltextBody = htmltextBody.replace("<guid>", "<guid><![CDATA[ ");
htmltextBody = htmltextBody.replace("</guid>", "]]></guid>");
htmltextBody = htmltextBody.replace("&", "&");
htmltext = htmltextHeader + htmltextBody;
Document doc = XMLfunctions.XMLfromString(htmltext);
WebRequest.java
package com.nesim.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
public class WebRequest {
public enum PostType{
GET, POST;
}
public String _url;
public String response = "";
public PostType _postType;
CookieStore _cookieStore = new BasicCookieStore();
public WebRequest(String url) {
_url = url;
_postType = PostType.POST;
}
public WebRequest(String url, CookieStore cookieStore) {
_url = url;
_cookieStore = cookieStore;
_postType = PostType.POST;
}
public WebRequest(String url, PostType postType) {
_url = url;
_postType = postType;
}
public String Get() {
HttpClient httpclient = new DefaultHttpClient();
try {
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, _cookieStore);
HttpResponse httpresponse;
if (_postType == PostType.POST)
{
HttpPost httppost = new HttpPost(_url);
httpresponse = httpclient.execute(httppost, localContext);
}
else
{
HttpGet httpget = new HttpGet(_url);
httpresponse = httpclient.execute(httpget, localContext);
}
StringBuilder responseString = inputStreamToString(httpresponse.getEntity().getContent());
response = responseString.toString();
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
return response;
}
private StringBuilder inputStreamToString(InputStream is) throws IOException {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is,Charset.forName("iso-8859-9")));
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
// Return full string
return total;
}
}
重要的:
不要忘记在 WebRequest.java 的第一行更改包名称
包 com.nesim.test;
结果:
进行这些更改后,您将获得以下内容:
D/title: Two Walk-Off Moments Lead To Two A&T Losses
D/description: The Lancers win in their last at-bat in both games of Saturday's doubleheader.
D/title: A&T To Play Four Against Longwood
D/description: A&T baseball takes a break from conference play this weekend.
D/title: Wilkerson Named MEAC Rookie of the Week
D/description: Wilkerson was 6-for-14 for the week of April 9-15.
D/title: Lights, Camera, Action
D/description: A&T baseball set to play nationally televised game on ESPNU.
D/title: Resilient Aggies Fall To USC Upstate
D/description: Luke Tendler extends his hitting streak to 10 games.
您的解析返回这些:
D/title : Two Walk-Off Moments Lead To Two A
D/description: The Lancers win in their last at-bat in both games of Saturday's doubleheader.
D/title : A
D/description: A&T baseball takes a break from conference play thisweekend.
D/title : Wilkerson Named MEAC Rookie of the Week
D/description: Wilkerson was 6-for-14 for the week of April 9-15.
D/title : Lights, Camera, Action
D/description: A&T baseball set to play nationally televised game on ESPNU.
D/title : Resilient Aggies Fall To USC Upstate
D/description: Luke Tendler extends his hitting streak to 10 games.