如何在Java中将转义字符作为文本读取? public List readRSS(字符串feedUrl、字符串openTag、字符串closeTag) 抛出IOException,MalformedURLException{ URL=新URL(feedUrl); BufferedReader=新的BufferedReader(新的InputStreamReader(url.openStream()); 串电流线; List templast=new ArrayList(); while((currentLine=reader.readLine())!=null){ 整数tagEndIndex=0; 整数tagStartIndex=0; 而(tagStartIndex>=0){ tagStartIndex=currentLine.indexOf(openTag,tagEndIndex); 如果(tagStartIndex>=0){ tagEndIndex=currentLine.indexOf(closeTag,tagStartIndex); add(currentLine.substring(tagStartIndex+openTag.length(),tagEndIndex)+“\n”); } } } if(templast.size()>0){ if(openTag.contains(“title”)){ 圣殿骑士。移除(0); 圣殿骑士。移除(0); } else if(openTag.contains(“desc”)){ 圣殿骑士。移除(0); } } 返回圣殿骑士; }
我写这段代码是为了阅读RSS提要。这一切都很好,但当解析器发现这样的字符时代码>它会断开。这是因为它找不到结束标记,因为xml被转义了如何在Java中将转义字符作为文本读取? public List readRSS(字符串feedUrl、字符串openTag、字符串closeTag) 抛出IOException,MalformedURLException{ URL=新URL(feedUrl); BufferedReader=新的BufferedReader(新的InputStreamReader(url.openStream()); 串电流线; List templast=new ArrayList(); while((currentLine=reader.readLine())!=null){ 整数tagEndIndex=0; 整数tagStartIndex=0; 而(tagStartIndex>=0){ tagStartIndex=currentLine.indexOf(openTag,tagEndIndex); 如果(tagStartIndex>=0){ tagEndIndex=currentLine.indexOf(closeTag,tagStartIndex); add(currentLine.substring(tagStartIndex+openTag.length(),tagEndIndex)+“\n”); } } } if(templast.size()>0){ if(openTag.contains(“title”)){ 圣殿骑士。移除(0); 圣殿骑士。移除(0); } else if(openTag.contains(“desc”)){ 圣殿骑士。移除(0); } } 返回圣殿骑士; },java,android,Java,Android,我写这段代码是为了阅读RSS提要。这一切都很好,但当解析器发现这样的字符时它会断开。这是因为它找不到结束标记,因为xml被转义了 我不知道如何在代码中修复它。有人能帮我解决这个问题吗 问题是特殊字符和#xD是一个换行符,因此您的开始和结束标记在不同的行上结束。因此,如果您逐行阅读,它将无法使用您拥有的代码 您可以尝试以下方法: public List<String> readRSS(String feedUrl, String openTag, String closeTag)
我不知道如何在代码中修复它。有人能帮我解决这个问题吗 问题是特殊字符
和#xD
是一个换行符,因此您的开始和结束标记在不同的行上结束。因此,如果您逐行阅读,它将无法使用您拥有的代码
您可以尝试以下方法:
public List<String> readRSS(String feedUrl, String openTag, String closeTag)
throws IOException, MalformedURLException {
URL url = new URL(feedUrl);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String currentLine;
List<String> tempList = new ArrayList<String>();
while ((currentLine = reader.readLine()) != null) {
Integer tagEndIndex = 0;
Integer tagStartIndex = 0;
while (tagStartIndex >= 0) {
tagStartIndex = currentLine.indexOf(openTag, tagEndIndex);
if (tagStartIndex >= 0) {
tagEndIndex = currentLine.indexOf(closeTag, tagStartIndex);
tempList.add(currentLine.substring(tagStartIndex + openTag.length(), tagEndIndex) + "\n");
}
}
}
if (tempList.size() > 0) {
if(openTag.contains("title")){
tempList.remove(0);
tempList.remove(0);
}
else if(openTag.contains("desc")){
tempList.remove(0);
}
}
return tempList;
}
给定此示例输入:
StringBuffer fullLine = new StringBuffer();
while ((currentLine = reader.readLine()) != null) {
int tagStartIndex = currentLine.indexOf(openTag, 0);
int tagEndIndex = currentLine.indexOf(closeTag, tagStartIndex);
// both tags on the same line
if (tagStartIndex != -1 && tagEndIndex != -1) {
// process the whole line
tempList.add(currentLine);
fullLine = new StringBuffer();
// no tags on this line but the buffer has been started
} else if (tagStartIndex == -1 && tagEndIndex == -1 && fullLine.length() > 0) {
/*
* add the current line to the buffer; it is part
* of a larger line
*/
fullLine.append(currentLine);
// start tag is on this line
} else if (tagStartIndex != -1 && tagEndIndex == -1) {
/*
* line started but did not have an end tag; add it to
* a new buffer
*/
fullLine = new StringBuffer(currentLine);
// end tag is on this line
} else if (tagEndIndex != -1 && tagStartIndex == -1) {
/*
* line ended but did not have a start tag; add it to
* the current buffer and then process the buffer
*/
fullLine.append(currentLine);
tempList.add(fullLine.toString());
fullLine = new StringBuffer();
}
}
对于desc
:
<title>another 
title 0</title>
<title>another title 1</title>
<title>another title 2</title>
<title>another title 3</title>
<title>another title 4</title>
<title>another 
another line in between 
title 5</title>
说明0
另一个
;说明1
您应该在完整的RSS提要上测试这种方法的性能。还请注意,特殊字符将不会转义。所以您希望将转义字符作为文本读取,然后(可能)跳过它们,对吗?@progyamer是的,我想跳过它们。现在发生的情况如下:RSS阅读器看到一个 ;然后退出读取,这样它就永远不会到达标签并崩溃。用图像更新了OP,使其更清晰。它是一个解析器,所以当遇到转义序列时,它会执行它应该执行的操作。你需要以某种方式推翻这个规则,把所有的东西都当作文本来阅读;您对输入的后处理只会增加一点。@progyamer我理解问题:)。不管怎样,你有没有办法让我取得成功fix@tima干得好
<title>another 
title 0</title>
<title>another title 1</title>
<title>another title 2</title>
<title>another title 3</title>
<title>another title 4</title>
<title>another 
another line in between 
title 5</title>
<desc>description 0</desc>
<desc>another 
description 1</desc>