Android中SAX XML解析 [英] SAX XML Parsing in android

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

问题描述

XML code是

 < XML版本=1.0编码=UTF-8&GT?;
  &所述; OPML版本=1>
  < HEAD>
  <冠军>广播LT; /标题>
  <状态> 200℃/状态>
  < /头>
  <身体GT;
  <外形类型=链接文本=本地URL =HTTP:// ...............键=本地/>
  <外形类型=链接文本=音乐URL =HTTP:// ..............键=音乐/>
  <外形类型=链接文本=走URL =HTTP:// ....................键=走/>
  <外形类型=链接文本=体育URL =HTTP:// ...........键=体育/>
  <外形类型=链接文本=广场URL =HTTP:// ...............键=广场/>
  <外形类型=链接文本=口头URL =HTTP:// .............键=口头/>
  <外形类型=链接文本=播客URL​​ =HTTP:// .....................键=播客/&G​​T;
  < /身体GT;
  < / OPML>
 

解决方案

我不会将它们保存在一个载体不一定只有当你真的要为一个特殊目的。我想将它们保存在一个的HashMap ,而不是让您可以参考他们通过自己的钥匙吧。

我要看看你的XML结构,以帮助您一个深入的答案。

编辑:这里是你的答案。晚但仍。

鉴于你的XML结构看起来是这样的。

 < XML版本=1.0编码=UTF-8&GT?;
&所述; OPML版本=1>
    < HEAD>
        <冠军>广播LT; /标题>
    < /头>
    <身体GT;
        <外形类型=链接文本=本地URL =htt​​p://google.at键=本地/>
        <外形类型=链接文本=音乐URL =htt​​p://google.at键=音乐/>
        <外形类型=链接文本=走URL =htt​​p://google.at键=走/>
        <外形类型=链接文本=体育URL =htt​​p://google.at键=体育/>
        <外形类型=链接文本=广场URL =htt​​p://google.at键=广场/>
        <外形类型=链接文本=口头URL =htt​​p://google.at键=口头/>
        <外形类型=链接文本=播客URL​​ =htt​​p://google.at键=播客/&G​​T;
    < /身体GT;
< / OPML>
 

要得到所有你需要解析为数据的有用的HashMap 的处理程序可能看起来像这样。

 进口的java.util.HashMap;

进口org.xml.sax.Attributes;
进口org.xml.sax.SAXException;
进口org.xml.sax.helpers.DefaultHandler中;

公共类MyContentHandler扩展的DefaultHandler {

    私人的HashMap<字符串,对象> outlineMap;
    私人的HashMap<字符串,字符串> outlineData;
    私人字符串键;

    公共无效startDocument()抛出的SAXException {
        outlineMap =新的HashMap<字符串,对象>();
    }

    公共无效调用endDocument()抛出的SAXException {
        AnotherClass.setHashMap(outlineMap);
    }

    公共无效的startElement(URI字符串,字符串的localName,字符串QNAME,
        属性的ATT)抛出的SAXException {
        如果(qName.equalsIgnoreCase(纲目)){
            outlineData =新的HashMap<字符串,字符串>();
            键= atts.getValue(钥匙);
            outlineData.put(类型,atts.getValue(类型));
            outlineData.put(文字,atts.getValue(文本));
            outlineData.put(URL,atts.getValue(URL));
        }
    }

    公共无效的endElement(URI字符串,字符串的localName,字符串QNAME)
    抛出的SAXException {
        如果(qName.equalsIgnoreCase(纲目)){
            outlineMap.put(键,outlineData);
        }
    }
}
 

HashMap的 outlineMap 将持有的所有大纲条目和HashMap的 outlineData 将持有的属性中的每一个大纲标签。字符串的定义,因为我们需要它来拿到钥匙,并正确设置它们的每一个outlineData。

正如你可以看到outlineMap总是在 startDocument()办法宣称这种方式,我们确保每一个使用此处理程序的时间你解析你将有一个新的空HashMap中。

的startElement()方法,我们检查标签的限定名等于大纲而忽略了案例吧。如果出现这种标签,我们宣布一个新的HashMap这样我们就可以进行设置每一个轮廓标记的每个属性。然后,我们通过解析大纲标签的属性键的键值赋值给我们的密钥字符串。然后我们通过所有其他有趣的属性,我们的 outlineData HashMap中使用把()方法。这种方法只接受一个字符串作为一个键和一个字符串值根据我们的定义。

现在我们进入到我们的的endElement()方法,该方法还检查外形发生再次无视外壳。如果发生了它,我们从前面的关键和outlineData HashMap中的值设置密钥字符串设置你的第一个outlineMap条目的内容。在这里,我们的HashMap接受一个字符串作为它的键和一个对象作为它的值(它接受几乎一切,它作为一切都在Java值,其实是在一个对象)。

现在,你有你准备使用HashMap中充满了所分析的数据。

在我的例子中,我通过了最后的HashMap outlineMap给设置在另一个类中调用endDocument()方法。这意味着,当在解析完成

下面是一个简短的说明如何SAX解析器使用这个方法让你拥有更好发生了什么线索。

 打开文档启动
    startDocument()被调用

ON开始标记:< TAG>或LT; TAG属性=123>
    的startElement()的起始标记出现时调用。
    在这里你可以决定来看看,哪些属性,它们的标签
    解析。

ON INNERTAG数据:DATA
    字符()被调用构建字符的字符数组。

ON结束标签:LT; / TAG>
    的endElement()结束标记出现时调用。

就文件END
    调用endDocument()被调用
 

当然

有几个可用的其他方法,但对于你这个方法是更有趣的现在。字符的方法可能不是那么有趣您的实际需要寿。

我希望它能帮助。如果您需要了解更多的只是要求按注释。

XML Code is

  <?xml version="1.0" encoding="UTF-8" ?> 
  <opml version="1">
  <head>
  <title>Radio</title> 
  <status>200</status> 
  </head>
  <body>
  <outline type="link" text="Local" URL="http://..............." key="local" /> 
  <outline type="link" text="Music" URL="http://.............." key="music" /> 
  <outline type="link" text="walk" URL="http://...................." key="walk" /> 
  <outline type="link" text="Sports" URL="http://..........." key="sports" /> 
  <outline type="link" text="Place" URL="http://..............." key="Place" /> 
  <outline type="link" text="Verbal" URL="http://............." key="Verbal" /> 
  <outline type="link" text="Podcasts" URL="http://....................." key="podcast" /> 
  </body>
  </opml>

解决方案

I wouldn't save them in a vector necessarily only if you really have to for a special purpose. I'd save them in a HashMap instead so you can reference them by their keys instead.

I need to see your XML structure to help you with an in depth answer.

EDIT: Here is your answer. Late but still.

Given your XML structure looks like this.

<?xml version="1.0" encoding="UTF-8" ?>
<opml version="1">
    <head>
        <title>Radio</title>
    </head>
    <body>
        <outline type="link" text="Local" URL="http://google.at" key="local" />
        <outline type="link" text="Music" URL="http://google.at" key="music" />
        <outline type="link" text="walk" URL="http://google.at" key="walk" />
        <outline type="link" text="Sports" URL="http://google.at" key="sports" />
        <outline type="link" text="Place" URL="http://google.at" key="Place" />
        <outline type="link" text="Verbal" URL="http://google.at" key="Verbal" />
        <outline type="link" text="Podcasts" URL="http://google.at" key="podcast" />
    </body>
</opml>

To get all the data you need parsed into a useful HashMap the Handler could look like this.

import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyContentHandler extends DefaultHandler {

    private HashMap<String, Object> outlineMap;
    private HashMap<String, String> outlineData;
    private String key;

    public void startDocument() throws SAXException {
        outlineMap = new HashMap<String, Object>();
    }

    public void endDocument() throws SAXException {
        AnotherClass.setHashMap(outlineMap);
    }

    public void startElement(String uri, String localName, String qName,
        Attributes atts) throws SAXException {
        if(qName.equalsIgnoreCase("OUTLINE")) {
            outlineData = new HashMap<String, String>();
            key = atts.getValue("key");
            outlineData.put("type", atts.getValue("type"));
            outlineData.put("text", atts.getValue("text"));
            outlineData.put("URL", atts.getValue("URL"));
        }
    }

    public void endElement(String uri, String localName, String qName)
    throws SAXException {
        if(qName.equalsIgnoreCase("OUTLINE")) {
            outlineMap.put(key, outlineData);
        }
    }
}

The HashMap outlineMap will hold all your outline entries and the HashMap outlineData will hold the attributes in every single outline tag. The String key is defined because we need it to get the keys and set them correctly for every outlineData.

As you can see outlineMap is always declared in the startDocument() method this way we ensure that every time your parse using this handler you will have an empty new HashMap.

In the startElement() method we check if the qualified name of the tag equals to OUTLINE while ignoring the case of it. If this tag occurs we declare a new HashMap so we can hold each attribute set of every outline tag. Then we assign a value to our key string by parsing the key value of the attribute key of the outline tag. Then we pass every other interesting attribute to our outlineData HashMap using the put() method. This method accepts only a string as a key and a string as a value by our definition.

Now we move on to our endElement() method which also checks for the occurrence of OUTLINE again ignoring the case. If it occurs we set the content of your first outlineMap entry by setting the key string from earlier as the key and the outlineData HashMap as the value. Here our HashMap accepts a string as its key and an object as its value (it accepts virtually everything as its value as everything in Java is an object in fact).

And now you have your ready to use HashMap filled with the parsed data.

In my example I pass our final HashMap outlineMap to a setter in another class in the endDocument() method. That means when the parsing is done.

Here is a short explanation how the SAX parser uses this methods so you have a better clue of whats happening.

ON DOCUMENT START
    startDocument() gets called

ON STARTING TAG : <TAG> or <TAG attribute="123">
    startElement() gets called when a starting tag appears.
    here you can decide which tag to look at and which attributes
    to parse.

ON INNERTAG DATA : DATA
    characters() gets called building a char array of characters.

ON END TAG : </TAG>
    endElement() gets called when the ending tag appears.

ON DOCUMENT END
    endDocument() gets called

Of course there are several other methods available but for you this methods are the more interesting ones right now. The characters method might not be that interesting for your actual needs tho.

I hope it helps. If you need to know more just ask by comment.

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

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