Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从SOAP响应获取XML文件作为值<;结果>;xml</结果>;_Android_Xml_Web Services_Parsing_Soap - Fatal编程技术网

Android 从SOAP响应获取XML文件作为值<;结果>;xml</结果>;

Android 从SOAP响应获取XML文件作为值<;结果>;xml</结果>;,android,xml,web-services,parsing,soap,Android,Xml,Web Services,Parsing,Soap,我从.NETWebService获取一个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:/

我从.NETWebService获取一个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>
HTTP/1.1200正常
内容类型:text/xml;字符集=utf-8
内容长度:长度
xml

我是android新手,我想知道是否有办法处理这个文件并从中读取数据??或者我应该只使用一些基本类型而不是xml文件吗?

有很多方法可以获取xml内容,我给你我的方法:

您需要前3种主要方法:

方法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;

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

}
public static String getValue(Element item, String str) {       
    NodeList n = item.getElementsByTagName(str);        
    return XMLfunctions.getElementValue(n.item(0));
}
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());
}
方法3:

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;

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

}
public static String getValue(Element item, String str) {       
    NodeList n = item.getElementsByTagName(str);        
    return XMLfunctions.getElementValue(n.item(0));
}
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());
}
使用:

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;

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

}
public static String getValue(Element item, String str) {       
    NodeList n = item.getElementsByTagName(str);        
    return XMLfunctions.getElementValue(n.item(0));
}
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());
}

这很好,但问题是xml不在web服务器上。他们在服务器上有一个web服务,通过从我的pc调用该服务,它会自动将xml文件下载到我的pc上(它不会在浏览器中打开,只是作为文件下载到我的pc上的下载文件夹中),我需要一种在安卓上获取该文件的方法。或者我应该改变WebServe我希望这会有所帮助,我需要在android设备上捕获文件,或者在最后尝试改变webservice:/