0

私は Android 用のアプリケーションを作成しており、このページの XML ファイルを表示する必要があります: In the application show Compra="481.3" Venta="485"but i cant "DOLAR SPOT INTERBANCARIO" and Var_por="-0,53" Var_pes="-2,60" hora="10:35". コードを教えてください。

XML イメージXML

これは ExampleHandler コードです

public class ExampleHandler extends DefaultHandler {

private boolean in_Root = false;
private boolean in_Registro = false;

private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();

public ParsedExampleDataSet getParsedData() {
    return this.myParsedExampleDataSet;
}


@Override
public void startDocument() throws SAXException {
    this.myParsedExampleDataSet = new ParsedExampleDataSet();
}

@Override
public void endDocument() throws SAXException {
}


@Override
public void startElement(String namespaceURI, String localName,
        String qName, Attributes atts) throws SAXException {
    if (localName.equals("Root")) {
        this.in_Root = true;
    }else if (localName.equals("Registro")) {
        this.in_Registro = true;
        // Extract an Attribute
        String attrValue = atts.getValue("Compra");
        Float compra = Float.parseFloat(attrValue);
        myParsedExampleDataSet.setExtractedCompra(compra);

        String attrValue2 = atts.getValue("Venta");
        Float venta = Float.parseFloat(attrValue2);
        myParsedExampleDataSet.setExtractedVenta(venta);

        **String attrValue3 = atts.getValue("Var_por");
        Float por = Float.parseFloat(attrValue3);
        myParsedExampleDataSet.setExtractedPor(por);**
        //its my wrong code for  Var_por    
    }
}

@Override
public void endElement(String namespaceURI, String localName, String qName)
        throws SAXException {
    if (localName.equals("Root")) {
        this.in_Root = false;
    }else if (localName.equals("Registro")) {

    }
}

/** Gets be called on the following structure: 
 * <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
    if(this.in_Registro){
        //myParsedExampleDataSet.setExtractedStruct(new String(ch, start, length));
    }
}

}

4

2 に答える 2

0

そのような小さなものについては、SimpleXML を確認してください。Androidでうまく動作します。

SimpleXML パーサー

于 2012-07-27T14:59:32.943 に答える
0

最近Jsoupを使用して、この問題の解決策を実装しました。以下に概説するソリューションは、指定された URL からデータを取得し、それを文字列の配列に解析します。

設定

.jar ファイルはここにあります。

Android アプリに組み込む方法については、こちらをご覧ください。

実装

使用されるインポート:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

機能コード:

public static ArrayList<String> parseDocument() 
{
    ArrayList<String> values = new ArrayList<String>();

    Document doc;
    try {
        doc = Jsoup.connect("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=7009").get(); // Timeout is in milliseconds
        Elements nodes = doc.select("Registro");

        values.add( nodes.attr("tipo") );
        values.add( nodes.attr("Compra") );
        values.add( nodes.attr("Venta") );
        values.add( nodes.attr("Var_por") );
        // etc

    } catch (IOException e) {
        e.printStackTrace();
    }
    return values;
}

関数が返されたら、ArrayList から値を読み取るだけです。

于 2012-07-27T15:19:10.873 に答える