XML解析在Blackberry中

时间:2011-09-05 11:29:41

标签: blackberry xml-parsing

我想解析i got from this url

的XML

我正在做这样的解析

connection = (HttpConnection)Connector.open(_url);
//Build Documents Based on the File
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
builder.isValidating();
Document document = builder.parse(connection.openInputStream());
Element rootElement = document.getDocumentElement();
rootElement.normalize();
NodeList list = document.getElementsByTagName("current_conditions");
int check = list.getLength();
for(int i=0;i < check; i++){
    //NodeList children = list.item(i).getChildNodes();
    Node children = list.item(i).getFirstChild();
    String conditionData = new String();
    if (children.getNodeType()!= Node.TEXT_NODE){
        NamedNodeMap child = children.getAttributes();
        if(child.getNamedItem("data")!=null){
            conditionData = child.getNamedItem("data").getNodeValue();
            System.out.println("++++++++++++++++++++++++"+conditionData);
        }
    }           
}

//displayNode( rootElement, 0 );

}catch (Exception e) {
    // TODO: handle exception
    System.err.println("++++++++++++++++++"+e.getMessage());
}

请帮忙......

1 个答案:

答案 0 :(得分:1)

您的list.item(i)可能没有孩子,因此孩子将为空。无论如何,尝试使用来自bb样本的解析器。这是它的代码:XMLDemo code