使用jsoup解析xml

时间:2012-08-29 15:40:37

标签: android xml parsing jsoup

我需要使用Jsoup,XML解析这个xml,我不知道它是如何工作的Jsoup。我阅读了网页jsoup site,但我不明白如何解析。 需要帮助,我需要学习如何使用它。

我尝试用简单的XML解析器进行解析,但是向我显示了这个错误:第3行第10列错误:格式不正确(令牌无效)。

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

}

}

处理程序

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

1 个答案:

答案 0 :(得分:0)

不是你想用XML做什么,但用Jsoup解析它不是一个大问题:

// 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");

如果您将xml local作为文件,则可以使用Document doc = Jsoup.parse(file, "utf-8");代替Jsoup.connect(...)(确保设置正确的编码)。

注意: Jsoup Document!= DOM Document - 但是它能够执行您想要用XML / HTML做的所有事情