String to XML conversion after that parsing in and

2019-09-04 01:07发布

问题:

I am doing a HttpPost request as a return I am getting a XML I can make Toast of it I is in a String Code looks like this

try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://xx.xx.xx.xx/login.asmx/login");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        String responseBody = EntityUtils.toString(response.getEntity());
        Toast.makeText( getApplicationContext(),"responseBody:   "+responseBody,Toast.LENGTH_SHORT).show();

and the XML looks like

<root status="Y">

<mb mbcode="150201" mbname="AKASH KUNDU" branchid="1" pwd="admin"/>

</root>

I want to Parse this XML and store this data as a global variable.I Did not found any reverent example that is why I am posing I am new here Advance thanks.

回答1:

I found my solution

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://xx.xx.xx.xx/login.asmx/login");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        String responseBody = EntityUtils.toString(response.getEntity());


        //saving the file as a xml
        FileOutputStream fOut = openFileOutput("loginData.xml",MODE_WORLD_READABLE);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(responseBody);
        osw.flush();
        osw.close();

        //reading the file as xml
        FileInputStream fIn = openFileInput("loginData.xml");
        InputStreamReader isr = new InputStreamReader(fIn);
        char[] inputBuffer = new char[responseBody.length()];
        isr.read(inputBuffer);
        String readString = new String(inputBuffer);



        //getting the xml Value as per child node form the saved xml
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        //InputStream is = new ByteArrayInputStream(responseBody.getBytes("UTF-8"));
        InputStream is = new ByteArrayInputStream(readString.getBytes("UTF-8"));
        Document doc = db.parse(is);

        /*DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc=builder.parse(fIn, null);*/
        NodeList root=doc.getElementsByTagName("root");




        for (int i=0;i<root.getLength();i++) {
            loginStatus = "" + ((Element)root.item(i)).getAttribute("status");
        }


            if(loginStatus.equalsIgnoreCase("Y"))
            {
                NodeList mb=doc.getElementsByTagName("mb");
                for (int i=0;i<mb.getLength();i++) {
                    setMbcode("" + ((Element)mb.item(i)).getAttribute("mbcode"));
                    setMbname("" + ((Element)mb.item(i)).getAttribute("mbname"));
                    branchid  = "" + ((Element)mb.item(i)).getAttribute("branchid");
                    pwd       = "" + ((Element)mb.item(i)).getAttribute("pwd");

                }


回答2:

Try the Android developer guides, or http://developer.android.com/training/basics/network-ops/xml.html. It's also not too difficult to use google.