简体   繁体   中英

How can i parse these xml file values in android

I have a xml file...

<?xml version="1.0" ?> 
-<dict>
   <ID>USDUSD</ID> 
   <VALUE>0.00</VALUE> 
   <ID>USDGBP</ID> 
   <VALUE>0.6465</VALUE> 
   <ID>USDEUR</ID> 
   :
   :
</dict>

An i tried with sax parser with following code

private boolean getUpdatedCurrencyValue(Context context) {
    try {

        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */

        URL sourceUrl = new URL("http:..../currencyconv.php");
        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        XMLHandler myXMLHandler = new XMLHandler();
        xr.setContentHandler(myXMLHandler);
        InputSource is=new InputSource(sourceUrl.openStream());
        Log.d("DEBUG",""+is);
        xr.parse(is);
        if(myMap!=null){
              myMap.clear();
        }  


        for (int i = 0; i < XMLHandler.countriesNames.size(); i++) {
            myMap.put(XMLHandler.countriesNames.get(i), XMLHandler.currencyValues.get(i));
             log(XMLHandler.countriesNames.get(i)+"  "+ XMLHandler.currencyValues.get(i));
        }

    } catch (Exception e) {
        e.printStackTrace();
        isError=true;


    }
}



public class XMLHandler extends DefaultHandler{

int counter=0;
protected static ArrayList<String> backupCurrencyValues = new ArrayList<String>();

//protected static String[] currencyValues=new String[290];
protected static ArrayList<String> currencyValues = new ArrayList<String>();
protected static ArrayList<String> countriesNames = new ArrayList<String>();
   //   private String[] countriesNames=new String[290];
  public XMLHandler() {
    counter=0;
  }


@Override
public void characters(char ch[], int start, int length) {
        String temp=new String(ch,start,length); //  this function retrieves values a from a tag each time
        Log.d("CURRENCY","Original "+temp);

        }

}

This function retrieves values a from a tag each time

but want values based on tags ..Any help?

Do you mean retrieving tag attribute value from a tag?

You might want to refer to my current XML parsing articles series: https://xjaphx.wordpress.com/learning/tutorials/

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