0

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

我尝试使用简单的 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));
    }
}
4

1 回答 1

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 本地文件作为文件,则可以使用Document doc = Jsoup.parse(file, "utf-8");而不是Jsoup.connect(...)(让您设置正确的编码)。

注意: Jsoup Document!= DOM Document- 但是它能够做任何你想做的 XML / HTML

于 2012-08-29T17:24:26.947 回答