我有这个 XML 解析器,但它在文本字段中呈现 html 标签,我希望你能用正确的答案帮助编辑我的代码,谢谢。我不知道如何从中删除 HTML 标签。请帮忙,等待接受有效的答案。
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* @param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* @param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* @param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* @param Element node
* @param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
最佳答案
如果你想完全删除 html 标签:
方法一)
public String removeHtmlTags(String inStr) {
int index=0;
int index2=0;
while(index!=-1)
{
index = inStr.indexOf("<");
index2 = inStr.indexOf(">", index);
if(index!=-1 && index2!=-1){
inStr = inStr.substring(0, index).concat(inStr.substring(index2+1, inStr.length()));
}
}
return inStr;
}
方法二)
import android.text.Html;
public static String removeHtmlTags(String htmlString){
//Remove HTML tags
String noHTMLString = Html.fromHtml(htmlString).toString();
return noHTMLString;
}
您需要在此方法中调用 removeHtmlTags()
:
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ) {
if( child.getNodeType() == Node.TEXT_NODE ){
//removeHtmlTags()
return removeHtmlTags(child.getNodeValue());
}
}
}
}
return "";
}
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
//removeHtmlTags()
return removeHtmlTags(this.getElementValue(n.item(0)));
}
关于android - 删除 RSS 提要中的 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21940554/