简体   繁体   English

从SOAP响应中获取XML文件作为值 <BooksResult> XML </BooksResult>

[英]Getting XML File as a value from SOAP response <BooksResult>xml</BooksResult>

I'm getting an xml file as a value from .net webservice like shown here 我从.net网络服务获取一个xml文件作为值,如下所示

    HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetDataForRecommendedBooksResponse xmlns="http://tempuri.org/">
      <GetDataForRecommendedBooksResult>xml</GetDataForRecommendedBooksResult>
    </GetDataForRecommendedBooksResponse>
  </soap:Body>
</soap:Envelope>

I'm reltivly new to android and i'm wondering is there a way to handle this file and read the data from it?? 我是android的新手,我想知道有没有一种方法可以处理此文件并从中读取数据? Or should I just use some primitive types instead of a xml file?? 还是我应该只使用一些原始类型而不是xml文件?

There is many way to get xml content I give you my way : 我有很多方法可以获取xml内容:

You Need First 3 primary method : 您需要首先使用3种主要方法:

Method 1 : 方法1:

public static String getXML(String url){     
            String line = null;
            Log.d("-----URL STATE -----","Start getXML");
            try {
                URL u = new URL (url);
                HttpURLConnection huc =  ( HttpURLConnection )  u.openConnection (); 
                huc.setRequestMethod ("GET");  //OR  huc.setRequestMethod ("HEAD"); 
                huc.connect () ; 
                int code = huc.getResponseCode() ;
                System.out.println(code);
                Log.d("-----URL STATE -----","Checking URL");
                if (code==404){
                    line="Wrong URL";
                    Log.d("-----URL STATE -----"," Wrong URL"+code);
                }else{
                //-------------------
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    line = EntityUtils.toString(httpEntity);
                }

            } catch (UnsupportedEncodingException e) {
                line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
            } catch (MalformedURLException e) {
                line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
            } catch (IOException e) {
                line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
            }

            return line;

    }

Method 2 : 方法2:

public final static Document XMLfromString(String xml){

    Document doc = null;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is); 

    } catch (ParserConfigurationException e) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    }

    return doc;

}

Method 3 : 方法3:

public static String getValue(Element item, String str) {       
    NodeList n = item.getElementsByTagName(str);        
    return XMLfunctions.getElementValue(n.item(0));
}

Using : 使用:

String xml = getXML(url);
Document doc =XMLfromString(xml);
NodeList nodes = doc.getElementsByTagName(yourTAG); //TAG you want to get as String
ArrayList<String> TAGS=new ArrayList<String>();
for (int i = 0; i < nodes.getLength(); i++) {
Element e = (Element) nodes.item(i);
TAGS.add(getValue(e,yourTAG.toString());
}

Now you can use "TAGS" 现在您可以使用“ TAGS”

And Always Remember 'StrictMode.setThreadPolicy' before everything : 并始终记住一切之前的“ StrictMode.setThreadPolicy”

if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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