简体   繁体   中英

how to xml format string parsing data in android studio in java

I am trying in this code

String DeviceInfo= 
"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><DeviceInfo dc="" dpId="" mc="" mi="" rdsId="" rdsVer=""/>"
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    { super.onActivityResult(requestCode, resultCode, data); if (resultCode ==
            RESULT_OK) {
        Bundle b = data.getExtras();
        if (b != null) {
            String deviceInfo = b.getString("DEVICE_INFO", "");
            String pidData = b.getString("PID_DATA");

            String dnc = b.getString("DNC", "");

            String dnr = b.getString("DNR", "");

            // String deviceinfo = bundle.getString("DEVICE_INFO");
            Log.d("device ",""+deviceInfo);
            String rdServiceInfo = b.getString("RD_SERVICE_INFO", "");


        /*  *//*  if
            (!dnc.isEmpty() || !dnr.isEmpty())
            { showLogInfoDialog("Device Info", dnc + dnr + " " +
                    deviceInfo + rdServiceInfo);
            }
          {
                //showLogInfoDialog("Device Info","" +deviceInfo );
            }*//*
            try {
                DocumentBuilderFactory dbf =
                        DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(deviceInfo));

                Document doc = db.parse(is);
                NodeList nodes = doc.getElementsByTagName("employee");

                // iterate the employees
                for (int i = 0; i < nodes.getLength(); i++) {
                    Element element = (Element) nodes.item(i);

                    NodeList name = element.getElementsByTagName("value");
                    Element line = (Element) name.item(0);
                    System.out.println("value: " + getCharacterDataFromElement(line));

                    NodeList title = element.getElementsByTagName("title");
                    line = (Element) title.item(0);
                    System.out.println("Title: " + getCharacterDataFromElement(line));
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }*/







             text1.setText(pidData);
            text.setText(deviceInfo);

        } } }

in this xml format string

What i understood from you question is you need to know how to parse String xml data. And then map the values to the respective fields. So below is the sample method which will parse the xml from string and gives the elements one by one.

/**
 * 
 * @param inputXmlS is String xml Data 
 */
public void xmlParser(String inputXmlS){
    try {
        XmlPullParserFactory xmlFactory = XmlPullParserFactory.newInstance();
        xmlFactory.setNamespaceAware(true);
        XmlPullParser xmlPullParser = xmlFactory.newPullParser();
        xmlPullParser.setInput(new StringReader(inputXmlS));
        int event = xmlPullParser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            if(event == XmlPullParser.START_DOCUMENT) {
                Log.d(TAG,"Start document");
            } else if(event == XmlPullParser.START_TAG) {
                Log.d(TAG,"Start tag "+xmlPullParser.getName());
            } else if(event == XmlPullParser.END_TAG) {
                Log.d(TAG,"End tag "+xmlPullParser.getName());
            } else if(event == XmlPullParser.TEXT) {
                Log.d(TAG,"Text "+xmlPullParser.getText());
            }
            event = xmlPullParser.next();
        }
        Log.d(TAG,"End document");

    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
    }
}

So now you can call this method like below

String deviceInfo= "<?xml version="1.0" encoding="UTF-8" standalone="yes"?><DeviceInfo dc="" dpId="" mc="" mi="" rdsId="" rdsVer=""/>";
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{ super.onActivityResult(requestCode, resultCode, data); if (resultCode ==
        RESULT_OK) {
 xmlParser(deviceInfo);
 }
}

Thats it.

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