【问题标题】:Parsing using DOM parser使用 DOM 解析器进行解析
【发布时间】:2013-03-18 13:32:59
【问题描述】:
        Visit_all_class parser = new Visit_all_class(); 
        String xml = parser.Call3(URL); // getting XML 
        Document doc = parser.getDomElement(xml); // getting DOM element 

        NodeList nl = doc.getElementsByTagName(KEY_VISIT); 

        // looping through all item nodes <item> 
        for (int i = 0; i < nl.getLength(); i++) { 
            // creating new HashMap 

            HashMap<String, String> map = new HashMap<String, String>(); 
            Log.v("map","map" +map);
            Element e = (Element) nl.item(i); 
            // adding each child node to HashMap key => value 
            map.put(KEY_ACCOUNTNUMBER, parser.getValue(e, KEY_ACCOUNTNUMBER)); 
            map.put(KEY_LOCATION, parser.getValue(e, KEY_LOCATION)); 
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME)); 
            map.put(KEY_VISITID, parser.getValue(e, KEY_VISITID)); 
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_LAST, parser.getValue(e, KEY_LAST));
            map.put(KEY_PLANNED, parser.getValue(e, KEY_PLANNED));
            map.put(KEY_COMPLETION, parser.getValue(e, KEY_COMPLETION));
            map.put(KEY_START, parser.getValue(e, KEY_START));
            map.put(KEY_STATUS, parser.getValue(e, KEY_STATUS));


            // adding HashList to ArrayList 
            menuItems1.add(map);

我已经解析了来自 webservice 的值并将它们放在 menuItems 中。但是,我想从 web 服务中单独解析值并将其传递给为数据库创建的方法。

请建议如何从 web 服务中单独解析值的方法。

【问题讨论】:

    标签: android web-services xml-parsing


    【解决方案1】:

    这取决于org.w3c.dom.Document 的格式,但您使用org.w3c.dom.Node 接口中的方法。见here

    例如,如果您希望 XML 格式如下:

    ...
    <visit>
        <account_no>1234</account_no>
        <location>www.example.com/locaiton1</location>
        <name>Ammu</name>
    </visit>
    <visit>
        <account_no>4321</account_no>
        <location>www.example.com/locaiton2</location>
        <name>William</name>
    </visit>
    ...
    

    你会这样做:

    Element e = (Element) nl.item(i); 
    int accountNo = (int) e.getChildNodes().item(0).getTextContent();
    String location = e.getChildNodes().item(1).getTextContent();
    String name = e.getChildNodes().item(2).getTextContent();
    

    【讨论】:

      猜你喜欢
      • 2013-07-18
      • 1970-01-01
      • 2015-11-03
      • 2013-01-31
      • 1970-01-01
      • 2013-12-24
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多