У меня была такая же проблема, и я сделал это:
я сделал 2 функции, getxml () проверяет наличие дочерних узлов
и xmltagswtf () возвращает остаток кода
public String getxml(Node b){
String va = "";
if (b.hasChildNodes()==true){
int nodes = 0;
int a=b.getChildNodes().getLength();
while (nodes <a){
Node c = b.getChildNodes().item(nodes);
if(c.getNodeName()!="#text"){
va+="<"+c.getNodeName()+">"+getxml(c)+"</"+c.getNodeName()+">";
}else{
va+=getxml(c);
}nodes+=1;
}
}else{va=b.getTextContent();}
return va;
}
public String xmltagswtf(String xmlt, String what) throws SAXException, IOException, ParserConfigurationException{
InputStream is = new ByteArrayInputStream(xmlt.getBytes("UTF-8"));
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(is);
NodeList response = document.getElementsByTagName(what);
Node nod = response.item(0);
String nodos = "";
if (nod.hasChildNodes()==true){
int nodes = 0;
int a=nod.getChildNodes().getLength();
while (nodes <a){
Node c = nod.getChildNodes().item(nodes);
nodos+="<"+c.getNodeName()+">"+getxml(c)+"</"+c.getNodeName()+">";
nodes+=1;
}
}else{
nodos+=nod.getTextContent();
System.out.println(nodos);
}
return nodos;
}
затем сделайте что-то вроде этого:
String xml ="<hello><hai>welcome</hai></hello>";
String hai =xmltagswtf(xml, "hello");