2017-06-22 30 views
0

XMLを返すRESTサービスがありますが、一部のXMLコンテンツにはHTMLタグがあります。 XMLノードをループすると、コンテンツ内のHTMLを取り除く.getTextContent()メソッドしかないようです。 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

私はそれらの中にHTMLを持っていたし、その後UIが戻ってそれを変換しますノードのためにBase64を返すために、RESTサービスを取得し、終わりました。

関連する問題