1

SOAP 呼び出しを作成すると、次のような XML 文字列が返されます。

<?xml version="1.0" encoding="utf-8"?>
<sopenevelop>
     <soapbody>
         <ActualAnswer> I want to get whatever is in here </ActualAnswer>
     </soapbody>
</sopenevelop>

Androidでそのxmlから「 ActualAnswer 」を読み取るにはどうすればよいですか?

たとえば、次のようなことがわかりました: Android での XML 文字列の解析? しかし、回答を除外する方法については実際には述べていませんでした..そして、それを機能させることができませんでした..

C#ではこれを行うことができました.xmlanswerは上のxmlですが、Androidでは理解できません

XmlDocument xml = new XmlDocument();
xml.LoadXml (xmlanswer);
string loginanswer =  (xml.GetElementsByTagName ("ActualAnswer") [0].InnerText);
4

4 に答える 4

0

必要に応じてjDomを使用してください...

http://www.mkyong.com/java/how-to-read-xml-file-in-java-jdom-example/

PS:Linuxでは動作しません...

于 2013-10-01T10:43:03.637 に答える
0
SoapEnvelope result = (SoapEnvelope) envelope.getResponse();
Log.d("result : ", "" + result.toString());
String property_name= result.getProperty("ActualAnswer").toString();

または次のようにインデックスで取得できます。

String property_name= result.getProperty(0).toString();
于 2013-10-01T10:43:18.883 に答える
0

DOMでこれを試してください

public static String checkLoginStatus(String response)
        throws ParserConfigurationException, SAXException, IOException {
    String status = "";
    DocumentBuilder db = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder();
    InputSource is = new InputSource();
    Document doc = null;

    is.setCharacterStream(new StringReader(response));

    try {
        doc = db.parse(is);
    } catch (SAXException e) {
        Log.d("Wrong XML file structure: ", e.getMessage());
        e.printStackTrace();
        throw e;

    } catch (IOException e) {
        Log.d("I/O exeption: ", e.getMessage());
        e.printStackTrace();
        throw e;
    }

    Node rootNode = doc.getElementsByTagName("soapbody").item(0);
    Element rootElement = (Element) rootNode;
    String status = getTagValue("ActualAnswer", rootElement);
    Log.d("StatusCode", status);
    return status;
}

public static String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
            .getChildNodes();
    Node nValue = (Node) nlList.item(0);
    if (nValue == null) {
        return "";
    } else {
        return nValue.getNodeValue();
    }
}
于 2013-10-01T10:46:34.217 に答える