java - SAX 解析 : how to fetch child nodes

标签 java android xml sax

我在 android 中使用 SAX 解析。对于以下 XML:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
    <channel>
        <title>Game Analysis</title>
        <item>
            <title>GTA</title>
            <description>ABC</description>
            <pubDate>Sat, 21 Feb 2012 05:18:23 GMT</pubDate>
            <enclosure type="audio/mpeg" url="http://URL.mp3" length="6670315"/>
        </item>
        <item>
            <title>CoD</title>
            <description>XYZ</description>
            <pubDate>Sat, 21 Feb 2011 05:18:23 GMT</pubDate>
            <enclosure type="audio/mpeg" url="http://URL.mp3" length="6670315"/>
        </item>
    </channel>
</rss>

我需要获取第一次出现的 <title> (就在下面)。

然后我需要再次从每个 block 中提取 <title> & <enclosure> .

我可以获取第一个 <title>使用: public void startElement(String uri, String localName, String qName, 属性属性)抛出 SAXException { 如果(qName.equals(“标题”)) ...

但是,我应该如何获取 <item> block 中的标签? ?

最佳答案

下面是我如何使用 SAX 实现的。

我已经修改了一口你的 XML 文件。

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
    <channel>
        <title>Game Analysis</title>
        <item>
            <title>GTA</title>
            <description>ABC</description>
            <pubDate>Sat, 21 Feb 2012 05:18:23 GMT</pubDate>
            <enclosure type="audio/mpeg" url="http://URL.mp3/1" length="6670315"/>
        </item>
        <item>
            <title>CoD</title>
            <description>XYZ</description>
            <pubDate>Sat, 21 Feb 2011 05:45:10 GMT</pubDate>
            <enclosure type="audio/mpeg" url="http://URL.mp3/2" length="6670345"/>
        </item>
        <item>
            <title>AtV</title>
            <description>fgh</description>
            <pubDate>Sat, 21 Feb 2011 06:20:10 GMT</pubDate>
            <enclosure type="audio/mpeg" url="http://URL.mp3/3" length="6670364"/>
        </item>
    </channel>
    <channel>
        <title>Game Analysis 2</title>
        <item>
            <title>GTA 2</title>
            <description>ABC 2</description>
            <pubDate>Sat, 21 Feb 2012 04:18:23 GMT</pubDate>
            <enclosure type="audio/mpeg" url="http://URL.mp3/2/1" length="6670315"/>
        </item>
        <item>
            <title>CoD 2</title>
            <description>XYZ 2</description>
            <pubDate>Sat, 21 Feb 2011 04:45:10 GMT</pubDate>
            <enclosure type="audio/mpeg" url="http://URL.mp3/2/2" length="6670345"/>
        </item>
        <item>
            <title>AtV 2</title>
            <description>fgh</description>
            <pubDate>Sat, 21 Feb 2011 05:20:10 GMT</pubDate>
            <enclosure type="audio/mpeg" url="http://URL.mp3/2/3" length="6670364"/>
        </item>
    </channel>
</rss>

实体

channel

public class Channel {

    private String title;
    private ArrayList<Item> alItems;

    public Channel(){}

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public ArrayList<Item> getAlItems() {
        return alItems;
    }

    public void setAlItems(ArrayList<Item> alItems) {
        this.alItems = alItems;
    }


}

外壳

public class Enclosure {

    private String type;
    private URL url;
    private Integer length;


    public Enclosure(){}

    public String getType() {
        return type;
    }


    public void setType(String type) {
        this.type = type;
    }


    public URL getUrl() {
        return url;
    }


    public void setUrl(URL url) {
        this.url = url;
    }


    public Integer getLength() {
        return length;
    }


    public void setLength(Integer length) {
        this.length = length;
    }




}

项目

public class Item {

    private String title;
    private String description;
    private String pubDate;
    private Enclosure enclosure;

    public Item(){}

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPubDate() {
        return pubDate;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }

    public Enclosure getEnclosure() {
        return enclosure;
    }

    public void setEnclosure(Enclosure enclosure) {
        this.enclosure = enclosure;
    }



}

处理程序

channel 处理器

public class ChannelHandler extends DefaultHandler{

    private ArrayList<Channel> alChannels;
    private Channel channel;
    private String reading;
    private ArrayList<Item> alItems;
    private Item item;
    private Enclosure enclosure;

    public ChannelHandler(){
        super();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        if(qName.equals("rss")){
                alChannels = new ArrayList<>();
        }
        else if(qName.equals("channel")){
            channel = new Channel();
        }
        else if(qName.equals("item")){
            item = new Item();
        }
        else if(qName.equals("enclosure")){

            enclosure = new Enclosure();
            enclosure.setType(attributes.getValue("type"));
            try {
                enclosure.setUrl(new URL(attributes.getValue("url")));
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            enclosure.setLength(Integer.parseInt(attributes.getValue("length")));

        }

    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        if(qName.equals("channel")){
            channel.setAlItems(alItems);
            alChannels.add(channel);
            alItems = null;
        }
        if(qName.equals("title")){

            if(alItems == null){
                channel.setTitle(reading);
                alItems = new ArrayList<>();
            }
            else if(item != null) {
                item.setTitle(reading);
            }

        }
        else if(qName.equals("item")){

            if(alItems != null){
                alItems.add(item);
                item = null;
            }

        }
        else if(qName.equals("description")){
            item.setDescription(reading);
        }
        else if(qName.equals("pubDate")){
            item.setPubDate(reading);
        }
        else if(qName.equals("enclosure")){
            item.setEnclosure(enclosure);
        }

    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        reading = new String(ch, start, length);
    }

    public ArrayList<Channel> getAlChannels() {
        return alChannels;
    }


}

经理

XMLManager

public final class XMLManager {


    public static ArrayList<Channel> getAlChannels(){
        ArrayList<Channel> alChannels = null;
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            SAXParser parser = factory.newSAXParser();
            File file = new File("D:\\Loic_Workspace\\TestSAX2\\res\\test.xml");
            ChannelHandler channelHandler = new ChannelHandler();
            parser.parse(file, channelHandler);
            alChannels = channelHandler.getAlChannels();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return alChannels;
    }

}

主体

MyMain

public class MyMain {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Enclosure enclosure = null;
        for(Channel channel : XMLManager.getAlChannels()){
            System.out.println("Channel title : "+channel.getTitle());
            System.out.println("------------------------");
            for(Item i:channel.getAlItems()){
                System.out.println(i.getTitle());
                System.out.println(i.getPubDate());
                System.out.println("Enclosure : ");
                enclosure = i.getEnclosure();
                System.out.println(enclosure.getType());
                System.out.println(enclosure.getUrl());
                System.out.println(enclosure.getLength());
                System.out.println("------------------------");
            }
        }




    }

}

控制台输出

Channel title : Game Analysis
------------------------
GTA
Sat, 21 Feb 2012 05:18:23 GMT
Enclosure : 
audio/mpeg
http://URL.mp3/1
6670315
------------------------
CoD
Sat, 21 Feb 2011 05:45:10 GMT
Enclosure : 
audio/mpeg
http://URL.mp3/2
6670345
------------------------
AtV
Sat, 21 Feb 2011 06:20:10 GMT
Enclosure : 
audio/mpeg
http://URL.mp3/3
6670364
------------------------
Channel title : Game Analysis 2
------------------------
GTA 2
Sat, 21 Feb 2012 04:18:23 GMT
Enclosure : 
audio/mpeg
http://URL.mp3/2/1
6670315
------------------------
CoD 2
Sat, 21 Feb 2011 04:45:10 GMT
Enclosure : 
audio/mpeg
http://URL.mp3/2/2
6670345
------------------------
AtV 2
Sat, 21 Feb 2011 05:20:10 GMT
Enclosure : 
audio/mpeg
http://URL.mp3/2/3
6670364
------------------------

所以它有效;)

关于java - SAX 解析 : how to fetch child nodes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14236767/

相关文章:

java - MediaRecorder.start() 抛出 IllegalStateException

java - 如何获取任何java文件的类名

java - 为什么 AsyncTask doInBackground 不运行但第二次运行

java - 如何使 IntelliJ/Android Studio 中的静态导入更快

android - 在 android 中使用 DayNight 主题的状态栏的颜色

java - 不带模式解码 xml

java - LDAP:使用Java获取目录中包含所有用户信息的列表

java - 在 Java 中测试 REST 服务

c# - 更多 XML 问题 - 未声明的实体 'nbsp'

xml - 像 Facebook 这样的网站使用什么格式来存储个人资料的数据?