Estou tentando converter um Xml
que já esta como string que recebo do servidor, porem no NodeList
o mesmo retorna vazio alguém pode me ajudar?
public String leXml(String xml, String url) throws Exception {
String numero = "";
System.out.println("lendo1");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
System.out.println("lendo2");
//Informamos qual arquivo xml vamos ler
Document doc = db.parse(new InputSource(xml));
//Criamos um objeto Element que vai receber as informacoes de doc
Element raiz = doc.getDocumentElement();
System.out.println("lendo3");
//Informamos qual tag vamos ler
NodeList endList = raiz.getElementsByTagName(url);
System.out.println("url " + url);
System.out.println("lendo4 " + endList.getLength());
for(int i = 0; i <= endList.getLength(); i++) {
Element endElement = (Element) endList.item(0);
System.out.println("lendo5");
//Passamos para o metodo getChildTagValue a tag Pai que
//iremos ler mais qual tag Filha será capturada
numero = getChildTagValue(endElement, "NumTT");
//String cpf = getChildTagValue(endElement, "cpf");
//String conta = getChildTagValue(endElement, "conta");
System.out.println("aee numTT: " + numero);
}
return numero;
}
public static String getChildTagValue(Element elem, String tagName) throws Exception {
NodeList children = elem.getElementsByTagName(tagName);
String result = null;
//children, a tag pai que estamos lendo,
// por exemplo a tag
if (children == null) {
return result;
}
//child, a tag que queremos recuperar o valor, por exemplo
//a tag
Element child = (Element) children.item(0);
if (child == null) {
return result;
}
//recuperamos o texto contido na tagName
result = child.getTextContent();
return result;
}
public String get(String getUrl) throws Exception {
URL url = new URL(getUrl);
String xmlRetorno;
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
OIChatBotLerXml lexml = new OIChatBotLerXml();
xmlRetorno = this.read(con.getInputStream());
System.out.println("leu " + xmlRetorno);
lexml.leXml(getUrl, xmlRetorno);
return xmlRetorno;
}
Xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Message MessageId="" MessageType="Integration Object" IntObjectName="PCS Saida IO" >
<ListOfPCSSaida>
<Dados>
<Codigo>10046</Codigo>
<Desc>Meio de Acesso não encontrado na Base do Siebel.</Desc>
<NumTT>201900000187290</NumTT>
</Dados>
</ListOfPCSSaida>
</Message>