简体   繁体   中英

How to get the desired data/values from the xml datatable?

Hi I am working on a client/server app. And I am able to get the response from the server in the xml format. but now I have to sort out or filter the data. I am using the following code to get the reply from the server and it is working fine

    package com.example.KsoapExample;

import android.util.Log;
import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.io.IOException;
import java.net.Proxy;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Sashen
 * Date: 12/17/13
 * Time: 10:58 AM
 * To change this template use File | Settings | File Templates.
 */
public class SoapRequests {

    private static final boolean DEBUG_SOAP_REQUEST_RESPONSE = true;
    //
    private static final String MAIN_REQUEST_URL ="http://192.168.0.7/xx/service.asmx";
     private static final String NAMESPACE = "http://tempuri.org/";
    private static final String SOAP_ACTION = "http://tempuri.org/";
    private static String SESSION_ID;
    public static String Toaster=null ;

    private final void testHttpResponse(HttpTransportSE ht) {
        ht.debug = DEBUG_SOAP_REQUEST_RESPONSE;
        if (DEBUG_SOAP_REQUEST_RESPONSE) {
            Log.v("SOAP RETURN", "Request XML:\n" + ht.requestDump);
            Log.v("SOAP RETURN", "\n\n\nResponse XML:\n" + ht.responseDump)
            ;
            Toaster=ht.responseDump;
        }
    }

    public String getCelsiusConversion(String fValue) {
        String data = null;
        String methodname = "GetServerStatus";

        SoapObject request = new SoapObject(NAMESPACE, methodname);
        request.addProperty("username", "admin");
        request.addProperty("password", "admin");

        SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);

        HttpTransportSE ht = getHttpTransportSE();
        try {
            ht.call(SOAP_ACTION + methodname, envelope);
            testHttpResponse(ht);
            SoapPrimitive resultsString = (SoapPrimitive)envelope.getResponse();

            List<HeaderProperty> COOKIE_HEADER = (List<HeaderProperty>) ht.getServiceConnection().getResponseProperties();

            for (int i = 0; i < COOKIE_HEADER.size(); i++) {
                String key = COOKIE_HEADER.get(i).getKey();
                String value = COOKIE_HEADER.get(i).getValue();

                if (key != null && key.equalsIgnoreCase("set-cookie")) {
                    SoapRequests.SESSION_ID = value.trim();
                    Log.v("SOAP RETURNCookiee", "Cookie :" + SoapRequests.SESSION_ID);
                    break;
                }
            }
            data = resultsString.toString();

        } catch (SocketTimeoutException t) {
            t.printStackTrace();
        } catch (IOException i) {
            i.printStackTrace();
        } catch (Exception q) {
            q.printStackTrace();
        }
        return data;
    }

    private final SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.implicitTypes = true;
        envelope.setAddAdornments(false);
        envelope.setOutputSoapObject(request);
        return envelope;
    }

    private final HttpTransportSE getHttpTransportSE() {
        HttpTransportSE ht = new HttpTransportSE(Proxy.NO_PROXY,MAIN_REQUEST_URL,60000);
        ht.debug = true;
        ht.setXmlVersionTag("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>");
        return ht;
    }

    private final List<HeaderProperty> getHeader() {
        List<HeaderProperty> header = new ArrayList<HeaderProperty>();
        HeaderProperty headerPropertyObj = new HeaderProperty("cookie", SoapRequests.SESSION_ID);
        header.add(headerPropertyObj);
        return header;
    }
}

and in log I can see the server is giving me data and also I am taking that data into a text field but it looks like the following

 <NewDataSet>
 <Table>
 <ServerStatus>Active</ServerStatus >
 </Table>
</NewDataSet>

Where as I want to take out and to show only "ServerStatus"that is eiter Active,Inactive etc. I have seen many techniques such as pullparser but they have there own Http connect method which I do not want to use. So Now tell me how to do that. Please help

I am getting the response and displaying it in textfield it is like

 <NewDataSet>
    <Table1>
    <ServerStatus>Standby</ServerStatus>
    </Table1>
    </NewDataSet>

Assume that you got the reply from the server and stored it in result .

String result = "<NewDataSet><Table><ServerStatus>Active</ServerStatus></Table></NewDataSet>";

  String status = "error";

  Document dom = null;
  DocumentBuilder builder;
  DocumentBuilderFactory factory;
  try {
    InputStream resultStream = new ByteArrayInputStream(result.getBytes("UTF-8"));
    factory = DocumentBuilderFactory.newInstance();
    builder = factory.newDocumentBuilder();
    dom = builder.parse(resultStream);
  } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
  } catch (ParserConfigurationException e) {
    e.printStackTrace();
  } catch (SAXException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
  if(dom != null) {
    NodeList nodeList = dom.getElementsByTagName("Table");
    for (int itr = 0; itr < nodeList.getLength(); itr++) {
      Node node = nodeList.item(itr);
      if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) node;
        status = eElement.getElementsByTagName("ServerStatus").item(0).getTextContent();
      }
    }
  }
  Log.d(TAG, status);

Imports:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM