2017-06-22 100 views
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; 
} 

回答

0

我结束了,得到了REST服务返回的Base64节点它们内部有HTML,然后用户界面将其转换回。