简体   繁体   中英

Parse XML file in Android

I am developing an App for online voting in which the login process is through webservice which is in dotnet . The webservice part is ready and working but I want to see what response is getting returned.

URL url=new URL("http://www.example.com/login");
            HttpURLConnection urlconnection=(HttpURLConnection)url.openConnection();
            String requestXML="<data><username>sachin.t@gmail.com</username><password>sachin</password></data>";
            urlconnection.setRequestMethod("POST");
            urlconnection.setDoInput(true);
            urlconnection.setDoOutput(true);
            urlconnection.setRequestProperty("Accept-Charset", "UTF-8");
            urlconnection.setRequestProperty("Content-type", "application/xml");
            urlconnection.setRequestProperty("Content-Length", ""+Integer.toString(requestXML.length()));

            //Send request
            DataOutputStream dout=new DataOutputStream(urlconnection.getOutputStream());
            dout.writeBytes(requestXML);
            dout.flush();
            dout.close();
            if(urlconnection.getResponseCode()==HttpURLConnection.HTTP_OK)
            {
                InputStream is=urlconnection.getInputStream();
                BufferedReader breader=new BufferedReader(new InputStreamReader(is));
                String temp;
                String resp="";
                while((temp=breader.readLine())!=null)
                {
                    resp+=temp;
                }



                Document respdoc=null;
                DocumentBuilderFactory docbuilderfactory=DocumentBuilderFactory.newInstance();
                DocumentBuilder docbuilder=docbuilderfactory.newDocumentBuilder();
                InputSource isource=new InputSource();
                isource.setCharacterStream(new StringReader(resp));
                respdoc=docbuilder.parse(isource);

From net I got this but I want to parse the response which is given below.

<login>
    <login_validation>"1/2/3"</login_validation> 
    ( 1-invalid username,2-successfull and 3- already voted)

    <voter_id>" 0/voter_id "</voter_id> 
    (if sucessfull then only voter_id else value will return 0)
</login>

Any suggestion ?

Thank You In advance.

You can use Xpath (http://developer.android.com/reference/javax/xml/xpath/package-summary.html). If I got you correctly your response is in "respdoc". To select login_validation node you have to do this:

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/login/login_validation";
Node loginValidation = (Node) xpath.evaluate(expression, respdoc, XPathConstants.NODE);

To select voter_id node:

expression = "/login/voter_id";
Node voterId = (Node) xpath.evaluate(expression, respdoc, XPathConstants.NODE);

After that you can do whatever you want with loginValidation and voterId.

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