如何做到与Android简单的XML解析器 [英] How to do a simple XML Parser with Android

查看:112
本文介绍了如何做到与Android简单的XML解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

地狱那里,我一直停留在这了一点吧。我知道这应该是简单,但我似乎无法找到我在哪里出了错。我建我的小XML解析器以下,并试图适应这里DOM解析器示例后:的 http://www.ibm.com/developerworks/opensource/library/x-android/index.html 我有认识的节点,但是我为我的生活,不能figureout为什么它告诉我的节点的值是空。帮助将不胜AP preciated。

我的XML测试文件。

 <?XML版本=1.0&GT?;
<&人GT;
    <名称>&斯科特LT; /名称>
    <性别与GT;男< /性别>
    <更多>和等..< /&更多GT;
< /人>

我的解析器code为。

 公共XMLParser类{
    InputStream的XmlDocument的;
    TextView的电视;    公共XMLParser的(InputStream中的XmlDocument,TextView的电视){
        this.xmlDocument = XmlDocument的;
        this.tv =电视;
    }    公众的HashMap<字符串,字符串>解析(){
        工厂的DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
        HashMap的<字符串,字符串> xmlItems =新的HashMap<字符串,字符串>();
        尝试{
            的DocumentBuilder建设者= factory.newDocumentBuilder();
            文档DOM = builder.parse(XmlDocument的);
            元根= dom.getDocumentElement();
            节点列表项= root.getElementsByTagName(人);
            元件rootElement的=(元件)items.item(0);
            项= rootElement.getChildNodes();
            tv.append(\\ nParser,项目#:+将String.valueOf(items.getLength()));
            的for(int i = 0; I< items.getLength();我++){
                节点项= items.item(I)
                xmlItems.put(item.getNodeName(),item.getNodeValue());
                tv.append(\\ NNM:+ item.getNodeName()+NV:+ item.getNodeValue());
            }
        }赶上(例外五){
            抛出新的RuntimeException(E);
        }
        返回xmlItems;
    }
}


解决方案

我使用XmlPullFactory,这不是所以坏了。

修改为转换到的Hashmap

请注意,这是不是真的建议。这code做的不可以为您在HashMap中重复键,它会覆盖任何现有的密钥!

 公开的HashMap<字符串,字符串> parseXml(字符串XML){
    XmlPullParserFactory厂;
    字符串标记名=;
    字符串文本=;
            HashMap的<字符串,字符串> HM =新的HashMap<字符串,字符串>();    尝试{
        工厂= XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(真);
        XmlPullParser XPP = factory.newPullParser();
        StringReader SR =新StringReader(XML);
        xpp.setInput(SR);
        INT EVENTTYPE = xpp.getEventType();        而(EVENTTYPE!= XmlPullParser.END_DOCUMENT){
            如果(EVENTTYPE == XmlPullParser.TEXT){
                文字= xpp.getText(); //拉出节点文本
            }否则如果(EVENTTYPE == XmlPullParser.END_TAG){
                标签名= xpp.getName();                                    hm.put(记名,文字);                文字=; //重置文本的下一个节点
            }
            EVENTTYPE = xpp.next();
        }
    }赶上(XmlPullParserException E){
        e.printStackTrace();
    }赶上(IOException异常五){
        e.printStackTrace();
    }赶上(例外五){
        Log.d(异常属性E +++标签名);
    }
}

Hell there, I have been stuck on this for a bit now. I know it should be simple but I can't seem to find where I went wrong. I built my little XML parser after following and trying to adapt the DOM Parser example here: http://www.ibm.com/developerworks/opensource/library/x-android/index.html I have it recognising the nodes but I, for the life of me, can't figureout why it is telling me the value of the nodes is "null". Help would be greatly appreciated.

My XML test file.

<?xml version="1.0"?>
<Person>
    <Name>Scott</Name>
    <Gender>Male</Gender>
    <More>And So On..</More>
</Person>

My Parser code is.

public class XMLParser {
    InputStream xmlDocument;
    TextView tv;

    public XMLParser(InputStream xmlDocument, TextView tv) {
        this.xmlDocument = xmlDocument;
        this.tv = tv;
    }

    public HashMap<String, String> parse() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        HashMap<String, String> xmlItems = new HashMap<String, String>();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document dom = builder.parse(xmlDocument);
            Element root = dom.getDocumentElement();
            NodeList items = root.getElementsByTagName("Person");
            Element rootElement = (Element)items.item(0);
            items = rootElement.getChildNodes();
            tv.append("\nParser, # of Items: " + String.valueOf(items.getLength()));
            for (int i = 0; i < items.getLength(); i++){
                Node item = items.item(i);
                xmlItems.put(item.getNodeName(), item.getNodeValue());
                tv.append("\nNM: " + item.getNodeName() + " NV: " + item.getNodeValue());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } 
        return xmlItems;
    }
}

解决方案

I'm using XmlPullFactory, and it's not so bad.

Edit for Converting to Hashmap

Note that this isn't really recommended. This code does not check for duplicate keys in the hashmap, and it will overwrite any existing keys!!!

public HashMap<String, String> parseXml(String xml) {
    XmlPullParserFactory factory;
    String tagName = "";
    String text = "";
            HashMap<String, String> hm = new HashMap<String, String>();

    try {
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        StringReader sr = new StringReader(xml);
        xpp.setInput(sr);
        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.TEXT) {
                text = xpp.getText(); //Pulling out node text
            } else if (eventType == XmlPullParser.END_TAG) {
                tagName = xpp.getName();

                                    hm.put(tagName, text);

                text = ""; //Reset text for the next node
            }
            eventType = xpp.next();
        }
    }  catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        Log.d("Exception attribute", e + "+" + tagName);
    }
}

这篇关于如何做到与Android简单的XML解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆