字符串转换XML在Android版之后解析 [英] String to XML conversion after that parsing in android

查看:96
本文介绍了字符串转换XML在Android版之后解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了 HttpPost 请求作为回报,我得到一个XML我可以用它来烤面包我是一个String code看起来像这样

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();

和XML看起来像

and the XML looks like

<根系状态=Y>

< MB MB code =150201mbname =AKASH昆都branchid =1PWD =管理/>

< /根>

我想这解析XML和存储这些数据作为一个全球性variable.I没有发现任何虔诚的例子,这就是为什么我冒充我是新来先谢谢了。

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.

推荐答案

我发现我的解决方案

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");

                }

这篇关于字符串转换XML在Android版之后解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆