0
我有一個REST服務返回XML,但在某些XML內容中有HTML標記。當我循環遍歷XML節點時,似乎只有一個.getTextContent()方法將內容中的HTML去除。有沒有剝離HTML的內容的另一種方式?從XML節點獲取HTML內容
我創建的函數來抓取XML節點內容和屬性。
來電:
String firstName = RESTHelper.getXMLProperty(xmlData, "rootNode/person/firstName");
的方法:
public static String getXMLProperty(String xml, String searchNodes) {
String retVal = "";
String[] nodeSplit = searchNodes.split("/");
if(xml.trim().length()==0)
return retVal;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc;
NodeList nList;
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xml)));
nList = doc.getElementsByTagName(nodeSplit[0]);
retVal = GetXMLNode(nList, searchNodes, 0);
} catch (Exception e) {
e.printStackTrace();
}
return retVal;
}
private static String GetXMLNode(NodeList nl, String searchNodes, int item)
{
String retVal = null;
String findNode = searchNodes.split("/")[item];
int count = searchNodes.split("/").length;
item++;
for(int i=0; i<nl.getLength(); i++) {
String foundNode = nl.item(i).getNodeName();
NamedNodeMap nnm = nl.item(i).getAttributes();
if(nnm!=null && nnm.getLength()>0 && count>item) {
Node attribute = nnm.getNamedItem(searchNodes.split("/")[item]);
if(attribute!=null) {
retVal = attribute.getTextContent(); //returns only text
break;
}
}
if(foundNode.equals(findNode) && count>item) {
retVal = GetXMLNode(nl.item(i).getChildNodes(), searchNodes, item);
break;
} else if(foundNode.equals(findNode) && count==item) {
retVal = nl.item(i).getTextContent(); //returns only text
break;
}
}
return retVal;
}