簡體   English   中英

如何從xml數據表中獲取所需的數據/值?

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

嗨,我正在開發客戶端/服務器應用程序。 而且我能夠以xml格式從服務器獲取響應。 但是現在我必須整理或過濾數據。 我正在使用以下代碼從服務器獲取答復,並且工作正常

    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;
    }
}

在日志中,我可以看到服務器正在向我提供數據,並且我也將這些數據帶入了文本字段,但是它看起來像以下內容

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

我想取出並僅顯示“ Active”,“非活動”等“ ServerStatus”的位置。我已經看到了許多技術,例如pullparser,但是它們有自己的Http connect方法,我不想使用。 所以現在告訴我該怎么做。 請幫忙

我正在獲取響應並將其顯示在文本字段中,就像

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

假設您從服務器獲得了答復並將其存儲在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);

進口:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM