0

I need parse this xml using Jsoup, XML and i dont know how it works Jsoup. I read the web page jsoup site but i don't understand how to parse. Need Help, i need to learn how to use it.

I tried parse with simple XML parser but show me this error: Error on line 3, column 10: not well-formed (invalid token).

public class xml4500 extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_xml4500);

 TextView tv = (TextView) findViewById(R.id.tv);


    try {
        URL url = new URL("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=4500");
        ExampleHandler4500 myExampleHandler = new ExampleHandler4500();
        Xml.parse(url.openStream(), Xml.Encoding.UTF_8, myExampleHandler);

        List<ParsedExampleDataSet4500> parsedExampleDataSetList = 
                myExampleHandler.getParsedData();

    for(ParsedExampleDataSet4500 parsedExampleDataSet : parsedExampleDataSetList){
            tv.append(parsedExampleDataSet.toString());

    }


    } catch(Exception e){
        tv.setText("Error:" + e.getMessage());
    }

}

}

Handler

public class ExampleHandler4500 extends DefaultHandler{

private boolean in_indices = false;
private boolean in_fecha = false;
private boolean in_indice = false;
private boolean in_encabezado = false;
private boolean in_columna = false;
private boolean in_titulo = false;
private boolean in_datos = false;
private boolean in_fila = false;



private StringBuilder mStringBuilder = new StringBuilder();


private ParsedExampleDataSet4500 mParsedExampleDataSet = new ParsedExampleDataSet4500();
private List<ParsedExampleDataSet4500> mParsedDataSetList = new ArrayList<ParsedExampleDataSet4500>();


public List<ParsedExampleDataSet4500> getParsedData(){
    return this.mParsedDataSetList;
}

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

@Override
public void endDocument() throws SAXException {

}

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    if (localName.equals("indices")){
        this.in_indices = true;

    }else if(localName.equals("fecha")){
        this.in_fecha = true;
    }else if(localName.equals("indice")){
        this.in_indice = true;
        String attrValue1 = attributes.getValue("nombre");
        String nombre = String.valueOf(attrValue1);
        mParsedExampleDataSet.setNombre(nombre);

    }else if(localName.equals("encabezado")){
        this.in_encabezado = true;
    }else if(localName.equals("columna")){
        this.in_columna = true;
    }else if(localName.equals("titulo")){
        this.in_titulo = true;
    }else if(localName.equals("datos")){
        this.in_datos = true;
    }else if(localName.equals("fila")){
        this.in_fila = true;



        this.mParsedExampleDataSet = new ParsedExampleDataSet4500();

    }
    mStringBuilder.setLength(0);

}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if(localName.equals("indices")){
        this.mParsedDataSetList.add(mParsedExampleDataSet);
    }else if(localName.equals("fecha")){
        this.in_fecha = false ;  
    }else if(localName.equals("indice")){
        this.in_indice = false;
    }else if(localName.equals("encabezado")){
        this.in_encabezado = false;
    }else if(localName.equals("columna")){
        this.in_columna = false;
    }else if(localName.equals("titulo")){
        this.in_titulo = false;
    }else if(localName.equals("datos")){
        this.in_datos = false;
    }else if(localName.equals("fila")){
        this.in_fila = false;

    }
    mStringBuilder.setLength(0);
}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    mStringBuilder.append(ch, start, length);
    if(this.in_indices){
        mParsedExampleDataSet.setIndices(new String(ch, start, length));    
    }else if(this.in_fecha){
        mParsedExampleDataSet.setFecha(new String(ch, start, length));  
    }else if(this.in_indice){

        mParsedExampleDataSet.setIndice(new String(ch, start, length));
    }else if(this.in_encabezado){
        mParsedExampleDataSet.setEncabezado(new String(ch, start, length));
    }else if(this.in_columna){
        mParsedExampleDataSet.setColumna(new String(ch, start, length));
    }else if(this.in_titulo){
        mParsedExampleDataSet.setTitulo(new String(ch, start, length));
    }else if(this.in_datos){
        mParsedExampleDataSet.setDatos(new String(ch, start, length));
    }else if(this.in_fila){
        mParsedExampleDataSet.setFila(new String(ch, start, length));
    }
}
vikiiii
  • 9,246
  • 9
  • 49
  • 68
edwin rojas
  • 139
  • 2
  • 9

1 Answers1

0

Not shure what you want to do with the XML, but parsing it with Jsoup is not a big problem:

// Parse the the website - further connection settings possible here
Document doc = Jsoup.connect("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=4500").get();

// Example - select the (first) 'fecha' tag
Element fecha = doc.select("fecha").first();

// Example - select all 'columna' tags and insert an attribute 'an' with value 'example'
Elements columna = doc.select("columna");
columna.attr("an", "example");

If you have the xml local as a file you can use Document doc = Jsoup.parse(file, "utf-8"); instead of Jsoup.connect(...) (make shure you set proper encoding).

Note: Jsoup Document != DOM Document - however its capable of doing everything you want to do with XML / HTML

ollo
  • 24,797
  • 14
  • 106
  • 155
  • all I want to do is display the xml in my android application – edwin rojas Aug 29 '12 at 17:46
  • You can get a String of the whole XML Document with `doc.toString()`, eg. `String xml = doc.toString()`. – ollo Aug 29 '12 at 18:10
  • You can [select](http://jsoup.org/cookbook/extracting-data/selector-syntax) every `Element(s)` you want and use `toString()` for getting a String of it. For values only use `text()` on an element. Best you take a look at the [codebook](http://jsoup.org/cookbook/) – ollo Aug 29 '12 at 18:31