如何将XML传入值保存到字符串中

时间:2013-11-22 09:37:21

标签: android

我是Android新手。我正在使用AsyncTaskYahoo weather API

获取值

我想从那里获得价值。我已经将值保存到String变量。但我不知道如何区分这些值。我想将这些值显示在文本视图中。我在下面用Logcat提到了我的代码。请帮帮我

enter image description here

1 个答案:

答案 0 :(得分:-1)

我不是要将您的Yahoo Weather xml转换为字符串,而是通过以下示例来指导它的可能性....

 public class XMLParser extends DefaultHandler
  {


            List<XmlValuesModel> list=null;

            // string builder acts as a buffer
            StringBuilder builder;

            XmlValuesModel jobsValues=null;


             // Initialize the arraylist
             // @throws SAXException

            @Override
            public void startDocument() throws SAXException {

                /******* Create ArrayList To Store XmlValuesModel object ******/
                list = new ArrayList<XmlValuesModel>();
            }


             // Initialize the temp XmlValuesModel object which will hold the parsed info
             // and the string builder that will store the read characters
             // @param uri
             // @param localName ( Parsed Node name will come in localName  )
             // @param qName
             // @param attributes
             // @throws SAXException

            @Override
            public void startElement(String uri, String localName, String qName,
                    Attributes attributes) throws SAXException {

                /****  When New XML Node initiating to parse this function called *****/

                // Create StringBuilder object to store xml node value
                builder=new StringBuilder();

                if(localName.equals("login")){

                    //Log.i("parse","====login=====");
                }
                else if(localName.equals("status")){

                    // Log.i("parse","====status=====");
                }
                else if(localName.equals("jobs")){

                }
                else if(localName.equals("job")){

                    // Log.i("parse","----Job start----");
                    /********** Create Model Object  *********/
                    jobsValues = new XmlValuesModel();
                }
            }



             // Finished reading the login tag, add it to arraylist
             // @param uri
             // @param localName
             // @param qName
             // @throws SAXException

            @Override
            public void endElement(String uri, String localName, String qName)
                    throws SAXException {


                if(localName.equals("job")){

                    /** finished reading a job xml node, add it to the arraylist **/
                    list.add( jobsValues );

                }
                else  if(localName.equalsIgnoreCase("status")){


                }
                else  if(localName.equalsIgnoreCase("id")){ 

                    jobsValues.setid(Integer.parseInt(builder.toString()));
                }
                else if(localName.equalsIgnoreCase("companyid")){

                    jobsValues.setcompanyid(Integer.parseInt(builder.toString()));
                }
                else if(localName.equalsIgnoreCase("company")){
                    jobsValues.setcompany(builder.toString());
                }
                else if(localName.equalsIgnoreCase("address")){
                    jobsValues.setaddress(builder.toString());
                }
                else if(localName.equalsIgnoreCase("city")){
                    jobsValues.setcity(builder.toString());
                }
                else if(localName.equalsIgnoreCase("state")){
                    jobsValues.setstate(builder.toString());
                }
                else if(localName.equalsIgnoreCase("zipcode")){
                    jobsValues.setzipcode(builder.toString());
                }
                else if(localName.equalsIgnoreCase("country")){
                    jobsValues.setcountry(builder.toString());
                }
                else if(localName.equalsIgnoreCase("telephone")){
                    jobsValues.settelephone(builder.toString());
                }
                else if(localName.equalsIgnoreCase("date")){
                    jobsValues.setdate(builder.toString());
                }

             // Log.i("parse",localName.toString()+"========="+builder.toString());
            }


             // Read the value of each xml NODE
             // @param ch
             // @param start
             // @param length
             // @throws SAXException

            @Override
            public void characters(char[] ch, int start, int length)
                    throws SAXException {

                /******  Read the characters and append them to the buffer  ******/
                String tempString=new String(ch, start, length);
                 builder.append(tempString);
            }
  }

最后应该有一个单独的类来保存你的xml值。例如,在您的情况下可能是:WeatherDetails.class