childNodes I need to get the value of the child nodes

0

The reason for my question is that I need to know how to obtain the information that has the child nodes, since I am consuming a web service and at the moment mapping the xml does not map it as I wish since I want to have the data of the xml separated as is MonthlyTax attached to the xml

<MortgageResults xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.webserviceX.NET/">
<MonthlyPrincipalAndInterest>0.083785411555805267</MonthlyPrincipalAndInterest>
<MonthlyTax>0.91666666666666663</MonthlyTax>
<MonthlyInsurance>0.083333333333333329</MonthlyInsurance>
<TotalPayment>1.0837854115558052</TotalPayment>
</MortgageResults>

I'm using Bonitasoft 7.3 with the groovy editor and I could not find information to do the mapping as you want to see if someone can help me indicating the procedure I have to perform or give me information about where I can clarify my doubts about this theme or mistakes you have, thanks

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.lang.Exception;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.lang.StackTraceElement;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter; 

String ejemplo = "";
String aux = "";
// Clean response xml document
responseDocumentBody.normalizeDocument();
// Get result node
NodeList resultList = responseDocumentBody.getElementsByTagName("*");
Element resultElement = (Element) resultList.item(0);
String weatherDataAsXML = resultElement.getTextContent();
aux  = aux +  (Element) resultList.item(0);

XStream xstream = new XStream();
String xml = xstream.toXML(aux);

//ejemplo = ejemplo + "--"+weatherDataAsXML;


// Check for empty result
if ("Data Not Found".equalsIgnoreCase(weatherDataAsXML))
	return null;

// Parse embedded XML of result
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new StringReader(aux));
try {
	Document weatherDataDocument = documentBuilder.parse(inputSource);
	Node weatherNode = weatherDataDocument.getDocumentElement();
	


// Save weather data
Map<Integer,String> data = new HashMap<Integer,String>();

NodeList childNodes = weatherNode.getChildNodes();

for (int i=0; i<childNodes.getLength(); i++)
{

		Node node = childNodes.item(i);
	
		if (node.getNodeType() == Node.ELEMENT_NODE)
		{
			int key = i;
			String value = node.getTextContent("*");// necesito  obtener el valor de los nodos hijos 
			ejemplo = ejemplo + "key ->"+key + " value ->"+value ; 
			data.put(key, value);
		}
}
}catch(Exception ex){
	StackTraceElement[] stackTraceElement =ex.getStackTraceElement(0);
		 int n = stackTraceElement.length;
		ejemplo = ejemplo +"---oooo---";
		for (int i = 0 ;i<n;i++ )
		{
		ejemplo = ejemplo+ " "+stackTraceElement[i].getLineNumber()+">>"+stackTraceElement[i].getMethodName();
		}
}
return ejemplo;
    
asked by esteban fabian patiño 26.09.2017 в 23:19
source

1 answer

1

Use the language XPath like this:

Document xmlDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(strXML);
XPath xpath=XPathFactory.newInstance().newXPath();

Node nodePrincipal=(Node)xpath.evaluate("/MortgageResults/MonthlyPrincipalAndInterest",xmlDoc,XPathConstants.NODE);
String principal=nodePrincipal.getTextContent().toUpperCase();

Node nodeTax=(Node)xpath.evaluate("/MortgageResults/MonthlyTax",xmlDoc,XPathConstants.NODE);
String tax=nodeTax.getTextContent().toUpperCase();
    
answered by 28.09.2017 / 01:49
source