解析XML在线文件Android

时间:2012-07-18 18:15:22

标签: android xml xml-parsing

我正在为Android制作应用程序,我需要显示此页面的XML文件:http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=7009 我尝试了页面上给出的解决方案,但我发现它错了,因为在运行应用程序时它不会显示。我只想展示“tipo = DOLAR SPOT INTERCAMBIO”

这是XML CODE

XML CODE

这是我的代码:

xmlpruebaprueba.jar

XMLdataCollected sitesList= null;

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


    //creando un Layout
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(1);

    //creando TextView

    TextView Registro[];
    TextView Tipo[];

    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        URL sourceURL = new URL("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=7009");

        handlingXml  HandlingXml = new handlingXml();
        xr.setContentHandler(HandlingXml);
        xr.parse(new InputSource(sourceURL.openStream()));
    }catch (Exception e){
        System.out.println("XML Parsing Exception= " + e);
    }


    sitesList = handlingXml.sitesList;

    Registro = new TextView[sitesList.getRegistro().size()];
    Tipo = new TextView[sitesList.getTipo().size()];

    for (int i = 0; i < sitesList.getRegistro().size(); i++) {
        Registro[i] = new TextView(this);
        Registro[i].setText("Registro = "+sitesList.getRegistro().get(i));
        Tipo[i] = new TextView(this);
        Tipo[i].setText("Tipo = "+sitesList.getTipo().get(i));

        layout.addView(Registro[i]);
        layout.addView(Tipo[i]);
    } 
} 

}


这是我的处理程序

Boolean currentElement = false;
String currentValue = null;
public static XMLdataCollected sitesList = null;


public static XMLdataCollected getDataCollected (){
    return sitesList;
}

public static void setSitesList(XMLdataCollected sitesList){
    handlingXml.sitesList = sitesList;
}

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    // TODO Auto-generated method stub

    currentElement = true;

    if(localName.equals("Root"))
    {
        sitesList = new XMLdataCollected();
    }else if (localName.equals("Registro")){
        String attr = attributes.getValue("tipo");
        sitesList.setTipo(attr);
    }
}



@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    // TODO Auto-generated method stub

    currentElement = false;


    if (localName.equalsIgnoreCase("Registro"))
        sitesList.setRegistro(currentValue);
        else if (localName.equalsIgnoreCase("Root"))
        sitesList.setRoot(currentValue);    
    }

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    // TODO Auto-generated method stub
    if (currentElement) {
        currentValue = new String(ch, start, length);
        currentElement = false;
        }
}

}

这是我的dataCollected

公共类XMLdataCollected {

private ArrayList<String> root = new ArrayList<String>();
private ArrayList<String> registro = new ArrayList<String>();
private ArrayList<String> tipo = new ArrayList<String>();

public ArrayList<String> getRoot (){
    return root;
}

public void setRoot(String root){
    this.root.add(root);
}

public ArrayList<String> getRegistro (){
    return registro;
}

public void setRegistro(String registro){
    this.registro.add(registro);
}

public ArrayList<String> getTipo (){
    return tipo; 
}

public void setTipo(String tipo){
    this.tipo.add(tipo);
}

}

1 个答案:

答案 0 :(得分:3)

您正在主UI线程上调用Web Request

请不要这样做

使用AsyncTask来调用您的请求。