builder.parse return null nice soft

1

I have an error on the line

Document doc = builder.parse(is);// doc  = null;

Where at the moment of using the builder.parse it becomes null which makes the error arise and is captured by the catch (Exception ex) saying that the doc is null, I am working with nice soft (version 7.3) and I am tried to consume a web service but when working with the xml does not allow me to advance more because of the error, thanks

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.lang.Exception;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

String ejemplo ="";
String ejemplo ="hola mundo  ";
String  aux1 ;
int n ;
boolean encontrado = false ;// bandera 

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	 
		// use the factory to create a documentbuilder
		DocumentBuilder builder = factory.newDocumentBuilder();
		// create a new document from input source
		InputSource is = new InputSource(new StringReader("weatherDataAsXML"));
		try {
		Document doc = builder.parse(is);// doc  = null;
		 // get the first element
		Element element = doc.getDocumentElement();
		 // get all child nodes
		NodeList nodes = element.getChildNodes();
		 // print the text content of each child
		 for (int i = 0; i < nodes.getLength(); i++) {
		 //   	
				ejemplo = ejemplo +""+ nodes.item(i).getTextContent();
				 
		}
		} catch (Exception ex) {
		
        StackTraceElement[] stackTraceElement =ex.getStackTraceElement(0);
        n = stackTraceElement.length;

        for (int i = 0 ;i<n;i++ )
		{
		ejemplo = ejemplo+ " "+stackTraceElement[i].getLineNumber()+">>"+stackTraceElement[i].getMethodName();
		}   
		} catch (SAXException e) {	
		e.printStackTrace();
		} catch (IOException e) {
		e.printStackTrace();
		}
		

return ejemplo;

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetMortgagePayment xmlns="http://www.webserviceX.NET/">
      <Years>10</Years>
      <Interest>0.7</Interest>
      <LoanAmount>1000000</LoanAmount>
      <AnnualTax>0.5</AnnualTax>
      <AnnualInsurance>0.9</AnnualInsurance>
    </GetMortgagePayment>
  </soap12:Body>
</soap12:Envelope>
    
asked by esteban fabian patiño 18.09.2017 в 22:21
source

1 answer

1

the solution could be done using the XStream external library

XStream xstream = new XStream(); 
String xml = xstream.toXML(aux); 
if ("Data Not Found".equalsIgnoreCase(weatherDataAsXML))
return null; 
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource inputSource = new InputSource(); 
inputSource.setCharacterStream(new StringReader(aux)); 
try {
Document weatherDataDocument = documentBuilder.parse(inputSource);

Since the problem I was having was that at the time of using the parse is not reading the paraméntro as if it were an xml for that reason it sends null

    
answered by 09.10.2017 / 16:07
source