且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用jsoup解析XML

更新时间:2023-02-23 16:50:43

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

  //解析网站 - 进一步连接设置可能在这里
文档的DOC = Jsoup.connect(http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=4500)获得();//例 - 选择(第一),出生日期标签
。元素出生日期= doc.select(出生日期)第一();//例 - 选择所有columna标签并插入一个属性'的,值为示例
元件columna = doc.select(columna);
columna.attr(一个,榜样);

如果你有本地的XML,你可以使用文件文档的DOC = Jsoup.parse(文件,UTF-8); 而不是 Jsoup.connect(...)(使舒尔你设置正确的编码)。

注意: Jsoup 文件 = DOM 文件 - 但是它能够做到的事情!一切你想用XML做/ HTML

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

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