1

我想按照模式解析数据

图案链接

我的代码如下

public class PriceXMLParsingExample extends Activity {

/** Create Object For SiteList Class */
PriceList sitesList = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /** Create a new layout to display the view */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(1);

    /** Create a new textview array to display the results */
    TextView name[];
    TextView website[];
    TextView category[];

    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://www.xmlcharts.com/cache/precious-metals.php");

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        PriceXMLHandler myXMLHandler = new PriceXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

    /** Get result from MyXMLHandler SitlesList Object */
    sitesList = PriceXMLHandler.sitesList;

    website = new TextView[sitesList.getWebsite().size()];

    /** Set the result text in textview and add it to layout */
    for (int i = 0; i < sitesList.getWebsite().size(); i++) {
        website[i] = new TextView(this);
        website[i].setText("Name = "+sitesList.getWebsite().get(i));

        layout.addView(website[i]);
    }

    /** Set the layout view to display */
    setContentView(layout);

}
}

PriceXMLHandler.java

public class PriceXMLHandler extends DefaultHandler {

Boolean currentElement = false;
String currentValue = null;
public static PriceList sitesList = null;

public static PriceList getSitesList() {
    return sitesList;
}

public static void setSitesList(PriceList sitesList) {
    PriceXMLHandler.sitesList = sitesList;
}
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    currentElement = true;

    if (localName.equals("prices"))
    {
        /** Start */ 
        sitesList = new PriceList();
    } else if (localName.equals("currency")) {
        /** Get attribute value */
        String attr = attributes.getValue("access");
        sitesList.setCategory(attr);
    }
    else if (localName.equals("price")) {
        String attr = attributes.getValue("access");
        sitesList.setCategory(attr);
    }

}

/** Called when tag closing ( ex:- <name>AndroidPeople</name> 
 * -- </name> )*/
@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    currentElement = false;

    /** set value */ 
    if (localName.equalsIgnoreCase("price"))
        sitesList.setName(currentValue);
    else if (localName.equalsIgnoreCase("currency"))
        sitesList.setWebsite(currentValue);

}

/** Called to get tag characters ( ex:- <name>AndroidPeople</name> 
 * -- to get AndroidPeople Character ) */
@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (currentElement) {
        currentValue = new String(ch, start, length);
        currentElement = false;
    }

}

}

PriceList.java

public class PriceList {

/** Variables */
private ArrayList<String> currency = new ArrayList<String>();
private ArrayList<String> price = new ArrayList<String>();
private ArrayList<String> access = new ArrayList<String>();


/** In Setter method default it will return arraylist 
 *  change that to add  */

public ArrayList<String> getName() {
    return currency;
}

public void setName(String name) {
    this.currency.add(name);
}

public ArrayList<String> getWebsite() {
    return price;
}

public void setWebsite(String website) {
    this.price.add(website);
}

public ArrayList<String> getCategory() {
    return access;
}

public void setCategory(String category) {
    this.access.add(category);
}

}

当我运行上面的代码时,我只得到银价而不是整个模式,那么我该如何解决它呢?

4

1 回答 1

1

开始了.....

价格.java

    package com.sof;

public class Price {
    private String accessp;
    private String pricevalue;

    public String getAccessp() {
        return accessp;
    }

    public void setAccessp(String access) {
        this.accessp = access;
    }

    public String getPricevalue() {
        return pricevalue;
    }

    public void setPricevalue(String pricevalue) {
        this.pricevalue = pricevalue;
    }

}

货币.java

    package com.sof;

import java.util.ArrayList;

public class Currency {
    private String accessc;
    private ArrayList<Price> prices=new ArrayList<Price>();

    public String getAccessc() {
        return accessc;
    }

    public void setAccessc(String access) {
        this.accessc = access;
    }

    public ArrayList<Price> getPrices() {
        return prices;
    }

    public void setPrices(ArrayList<Price> price) {
        this.prices = price;
    }


}

价格.java

    package com.sof;

import java.util.ArrayList;

public class Prices {

    private ArrayList<Currency> currencies=new ArrayList<Currency>();

    public ArrayList<Currency> getCurrencies() {
        return currencies;
    }

    public void setCurrencies(ArrayList<Currency> currencies) {
        this.currencies = currencies;
    }

    public void addCurrency(Currency currency)
    {
        this.currencies.add(currency);
    }


}

PriceXMLHandler.java

    package com.sof;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class PriceXMLHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = null;


    public  Prices pricesList=new Prices();
    public  Currency currency=null;
    public  Price price=null;

    boolean bprices = false;
    boolean bcurrency = false;
    boolean bprice = false;




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

        currentElement = true;


        if (qName.equals("prices")) {
            bprices=true;

    //  System.out.println(qName);



        } else if (qName.equals("currency")) {
            bcurrency=true;
            currency=new Currency();

            String attr = attributes.getValue("access");
            currency.setAccessc(attr);

        } else if (qName.equals("price")) {
            bprice=true;
            price=new Price();


            //System.out.println(qName);
            String attr = attributes.getValue("access");
            price.setAccessp(attr);


        }

    }



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

        currentElement = false;

        /** set value */
        if (qName.equalsIgnoreCase("price"))
        {


        }
        else if (qName.equalsIgnoreCase("currency"))
        {

            pricesList.getCurrencies().add(currency);
        }
    }


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

        if (bprice) {



            price.setPricevalue(new String(ch, start, length));
            currency.getPrices().add(price);





            bprice = false;
        }

    }

}

最后 是Tester.java

    package com.sof;

import java.io.FileInputStream;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class Tester {
    public static void main(String[] args) {

        Prices sitesList;
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            InputStream xmlInput = new FileInputStream("src/test.xml");
            SAXParser saxParser = factory.newSAXParser();
            PriceXMLHandler handler = new PriceXMLHandler();
            saxParser.parse(xmlInput, handler);
            sitesList = handler.pricesList;

            for(Currency currency:sitesList.getCurrencies())
            {
                System.out.println(currency.getAccessc());
                for(Price price:currency.getPrices())
                {
                    System.out.println(price.getAccessp()+"\t"+price.getPricevalue());

                }
            }


        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
            e.printStackTrace();
        }


    }

}

输出将是

    aud
gold    46.029190755073
palladium   26.341121448078
platinum    51.54654854858
silver  0.71732682032307
brl
gold    98.537459156603
palladium   56.390024161854
platinum    110.34879907615
silver  1.5356247003263
cad
gold    45.011219881119
palladium   25.758567334448
platinum    50.406557073289
silver  0.70146258725235
chf
gold    36.245806009071
palladium   20.742384613034
platinum    40.59046375746
silver  0.56486087085223
cny
gold    244.21529483123
palladium   139.75706796276
platinum    273.48852640726
silver  3.8058931308984
eur
gold    29.556496982569
palladium   16.914294251671
platinum    33.099330699618
silver  0.46061352921044
gbp
gold    24.402090312082
palladium   13.964582343362
platinum    27.327083364375
silver  0.3802863697071
inr
gold    2520.7993626485
palladium   1442.5776571022
platinum    2822.9587484909
silver  39.284570547915
jpy
gold    4148.483313
palladium   2374.052226
platinum    4645.74747
silver  64.650677
mxn
gold    537.06911300288
palladium   307.34849993124
platinum    601.44570550145
silver  8.3697773696277
rub
gold    1412.2617736624
palladium   808.1949364365
platinum    1581.5446434145
silver  22.008930223337
usd
gold    40.39419
palladium   23.11638
platinum    45.2361
silver  0.62951
zar
gold    448.9410405203
palladium   256.91545467956
platinum    502.75402980182
silver  6.9963743404171

如果您遇到任何问题,请告诉我...

于 2014-01-29T11:50:11.903 回答