【问题标题】:parse xml using jsoup使用jsoup解析xml
【发布时间】:2012-08-29 15:40:37
【问题描述】:

我需要使用 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));
    }
}

【问题讨论】:

    标签: android xml parsing jsoup


    【解决方案1】:

    不确定你想用 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 做的事情

    【讨论】:

    • 我要做的就是在我的android应用程序中显示xml
    • 您可以使用doc.toString() 获取整个XML 文档的字符串,例如。 String xml = doc.toString().
    • 您可以 select 每个您想要的 Element(s) 并使用 toString() 获取它的字符串。对于值,仅在元素上使用 text()。最好看看codebook
    猜你喜欢
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2013-02-01
    • 2014-07-30
    相关资源
    最近更新 更多