How to parse an XML response from the Web Service

2

Hello, I would like to know how I parse an xml response that I receive from my web service ..

In case, I'm doing in Android, and wanted to transform the values and names that display in xml, into variables to be applied a graph with them ..

WS's response would be this:

<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="GWDataTable" msdata:UseCurrentLocale="true">
<xs:complexType> 
<xs:choice minOccurs="0" maxOccurs="unbounded"> 
<xs:element name="GWDataTable"> 
<xs:complexType> 
<xs:sequence> 
<xs:element name="CODFORMAPG" type="xs:string" minOccurs="0" /> 
<xs:element name="DESCRICAO" type="xs:string" minOccurs="0" /> 
<xs:element name="VALOR" type="xs:decimal" minOccurs="0" /> 
</xs:sequence> 
</xs:complexType> 
</xs:element> 
</xs:choice> 
</xs:complexType> 
</xs:element> 
</xs:schema> 
<GWDataTable> 
<VALOR>462.73</VALOR> 
</GWDataTable> <GWDataTable> 
<CODFORMAPG>1 </CODFORMAPG> 
<DESCRICAO>DINHEIRO</DESCRICAO> 
<VALOR>38502.39</VALOR> 
</GWDataTable> <GWDataTable> 
<CODFORMAPG>2 </CODFORMAPG> 
<DESCRICAO>CHEQUE</DESCRICAO> 
<VALOR>3765.63</VALOR> 
</GWDataTable> <GWDataTable> 
<CODFORMAPG>3 </CODFORMAPG> 
<DESCRICAO>CARTOES</DESCRICAO> 
<VALOR>13043.70</VALOR> 
</GWDataTable> <GWDataTable> 
<CODFORMAPG>5 </CODFORMAPG> 
<DESCRICAO>TICKETS</DESCRICAO> 
<VALOR>6.50</VALOR> 
</GWDataTable> <GWDataTable> 
<CODFORMAPG>6 </CODFORMAPG> 
<DESCRICAO>NOTA PROMISSORIA</DESCRICAO> 
<VALOR>56.24</VALOR> 
</GWDataTable> <GWDataTable> 
<CODFORMAPG>9 </CODFORMAPG> 
<DESCRICAO>TEF</DESCRICAO> 
<VALOR>27.47</VALOR> 
</GWDataTable> 
</NewDataSet>

I would like to filter all descriptions and their respective values and store them in variables to be reused ..

    
asked by anonymous 25.09.2014 / 15:22

1 answer

1

I've already used part of an answer from another post that I had not found:

Parsing XML

The code edited by me is like this and working 100%:

package com.getway.infovendas.Parsers;

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException; 
import java.io.IOException;
import java.io.StringReader;
import org.w3c.dom.Document;
import com.getway.infovendas.FormasDePagamento;

public class pFormasDePagamento {

public static void parseXML(String xml) throws ParserConfigurationException, SAXException, IOException {
    InputSource is = new InputSource(new StringReader(xml));
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(is);

    doc.getDocumentElement().normalize();

    String valFormaOutros = doc.getElementsByTagName("VALOR").item(0).getTextContent();

    String descForma = doc.getElementsByTagName("DESCRICAO").item(0).getTextContent();
    String valForma = doc.getElementsByTagName("VALOR").item(1).getTextContent();
    String descForma1 = doc.getElementsByTagName("DESCRICAO").item(1).getTextContent();
    String valForma1 = doc.getElementsByTagName("VALOR").item(2).getTextContent();
    String descForma2 = doc.getElementsByTagName("DESCRICAO").item(2).getTextContent();
    String valForma2 = doc.getElementsByTagName("VALOR").item(3).getTextContent();
    String descForma3 = doc.getElementsByTagName("DESCRICAO").item(3).getTextContent();
    String valForma3 = doc.getElementsByTagName("VALOR").item(4).getTextContent();
    String descForma4 = doc.getElementsByTagName("DESCRICAO").item(4).getTextContent();
    String valForma4 = doc.getElementsByTagName("VALOR").item(5).getTextContent();
    String descForma5 = doc.getElementsByTagName("DESCRICAO").item(5).getTextContent();
    String valForma5 = doc.getElementsByTagName("VALOR").item(6).getTextContent();


    FormasDePagamento.tvFormas.setText("Outros: "+ valFormaOutros+ "\n"+ descForma + ": " + valForma + "\n" + descForma1 + ": " + valForma1 + "\n" + descForma2 + ": " + valForma2 + "\n" + descForma3 + ": " + valForma3 + "\n" + descForma4 + ": " + valForma4 + "\n" + descForma5 + ": " + valForma5);
}


}
    
26.09.2014 / 18:45