1
I am using the following code to read a file .xml
try {
File fXmlFile = new File("C:\\res\\teste.xml"); // C:/.../teste.xml
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbFactory.newDocumentBuilder();
Document doc = builder.parse(fXmlFile);
//doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("soapenv:Envelope");
System.out.println(nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++) {
Node mNode = nodeList.item(i);
System.out.println("jjj2");
if (mNode.getNodeType() == Node.ELEMENT_NODE) {
Element mElement = (Element) mNode;
String anoLido = "AnoSP: " + mElement.getElementsByTagName("apis:AnoSP").item(0).getTextContent();
System.out.println("AnoSP: " + mElement.getElementsByTagName("apis:AnoSP").item(0).getTextContent());
System.out.println("NumeroSP: " + mElement.getElementsByTagName("apis:NumeroSP").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
String anoLido = null;
return null;
But the return of System.out.println(nodeList.getLength());
is coming as 0 even with file .xml
containing the tags
XML file:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:apis="http://schemas.datacontract.org/2004/07/Integracao.Modelo.Chamada">
<soapenv:Body>
<tem:CancelaSP>
<!--Optional:-->
<tem:token>386922849949</tem:token>
<!--Optional:-->
<tem:cancelaSPIntegracao>
<!--Optional:-->
<apis:AnoSP>2016</apis:AnoSP>
<!--Optional:-->
<apis:NumeroSP>5656</apis:NumeroSP>
</tem:cancelaSPIntegracao>
</tem:CancelaSP>
</soapenv:Body>
</soapenv:Envelope>
I copied here and it worked perfectly 1 jjj2 Anosp: 2016 Numerosp: 5656
– Thiago Luiz Domacoski