0

I am making an application for Android and I need to display an XML file of this page: 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". Help me with the code please.

XML image XML

This is ExampleHandler code

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));
    }
}

}

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130

2 Answers2

0

You might check out SimpleXML for something small like that. It works well on Android.

SimpleXML parser

Kaediil
  • 5,465
  • 2
  • 21
  • 20
0

I implemented a solution to this problem recently using Jsoup. The solution outlined below will both fetch the data from the supplied URL and parse it into an array of Strings.

Setup

The .jar file can be found here.

Instructions on how to include it in an Android app are here.

Implementation

Imports used:

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

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

Function code:

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;
}

All you need to do once the function is returned is read the values from the ArrayList.

Community
  • 1
  • 1
Maurice Gavin
  • 2,047
  • 1
  • 20
  • 26