简体   繁体   English

在Java中使用SAX解析XML

[英]Parsing XML usin SAX in java

I have this code to parse XML data .. 我有这段代码来解析XML数据..

But when startelement and endelement functions are called they don't get the parameters values ( as name parameter don't have any data ) when tying to print the parameters values. 但是,当调用startelement和endelement函数时,它们在试图打印参数值时没有得到参数值(因为name参数没有任何数据)。 it dont't have any value, why ? 它没有任何价值,为什么?

I call updateArticle function in the following code 我在以下代码中调用updateArticle函数

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

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


    public class RSSHandler extends DefaultHandler {

            // Used to define what elements we are currently in
            private boolean inItem = false;
            private boolean inTitle = false;
            private boolean inLink = false;

            // Feed and Article objects to use for temporary storage
            private Article currentArticle = new Article();
            private Feed currentFeed = new Feed();

            // Number of articles added so far
            private int articlesAdded = 0;

            private ArrayList<Article> articles = new ArrayList<Article>(); 
            private ArrayList<Feed> feeds = new ArrayList<Feed>(); 
            // Number of articles to download
            private static final int ARTICLES_LIMIT = 15;

            // The possible values for targetFlag
            private static final int TARGET_FEED = 0;
            private static final int TARGET_ARTICLES = 1;

            // A flag to know if looking for Articles or Feed name
            private int targetFlag;


            public RSSHandler(){ }
            public void startElement(String uri, String name, String qName,Attributes atts) {
                    if (name.trim().equals("title"))
                            inTitle = true;
                    else if (name.trim().equals("item"))
                            inItem = true;
                    else if (name.trim().equals("link"))
                            inLink = true;
                    System.out.println(name.trim());
            }

            public void endElement(String uri, String name, String qName)throws SAXException {
                    if (name.trim().equals("title"))
                            inTitle = false;
                    else if (name.trim().equals("item"))
                            inItem = false;
                    else if (name.trim().equals("link"))
                            inLink = false;

                    // Check if looking for feed, and if feed is complete
                    if (targetFlag == TARGET_FEED && currentFeed.url != null && currentFeed.title != null) {
                            // We know everything we need to know, so insert feed and exit
                            System.out.println("add current feed");
                            feeds.add(currentFeed);
                         //   throw new SAXException();
                    }

                    // Check if looking for article, and if article is complete
                    if (targetFlag == TARGET_ARTICLES && currentArticle.url != null && currentArticle.title != null) {

                            Article article = new Article();
                            article.feedId = currentFeed.id;
                            article.title = currentArticle.title;
                            article.url = currentArticle.url;
                            System.out.print(article.title);
                            articles.add(article);

                            //store articles in database

                            currentArticle.title = null;
                            currentArticle.url = null;

                            // Lets check if we've hit our limit on number of articles
                            articlesAdded++;
                            if (articlesAdded >= ARTICLES_LIMIT)
                                    throw new SAXException();
                    }

            }
            public ArrayList<Article> getArticles(){
                return this.articles;
            }            
            public ArrayList<Feed> getFeeds(){
                return this.feeds;
            }
            public void characters(char ch[], int start, int length) {

                    String chars = (new String(ch).substring(start, start + length));
                    System.out.println(chars);
                    try {
                            // If not in item, then title/link refers to feed
                            if (!inItem) {
                                    if (inTitle)
                                            currentFeed.title = chars;

                            } else {

                                    if (inLink)
                                            currentArticle.url = new URL(chars);
                                    if (inTitle)
                                            currentArticle.title = chars;

                            }
                    } catch (MalformedURLException e) {
                    }

            }

            public void createFeed(URL url) {
                    try {
                            targetFlag = TARGET_FEED;
                            currentFeed.url = url;

                            SAXParserFactory spf = SAXParserFactory.newInstance();
                            SAXParser sp = spf.newSAXParser();
                            XMLReader xr = sp.getXMLReader();
                            xr.setContentHandler(this);
                            xr.parse(new InputSource(url.openStream()));

                    } catch (IOException e) {}
                    catch (SAXException e) {} 
                    catch (ParserConfigurationException e) {}
            }

            public void updateArticles(Feed feed) {
                    try {
                            targetFlag = TARGET_ARTICLES;
                            currentFeed = feed;
                            System.out.println(feed.url.toString());
                            SAXParserFactory spf = SAXParserFactory.newInstance();
                            SAXParser sp = spf.newSAXParser();
                            XMLReader xr = sp.getXMLReader();
                            xr.setContentHandler(this);
                            xr.parse(new InputSource(currentFeed.url.openStream()));

                    } catch (IOException e) {} 
                    catch (SAXException e) {} 
                    catch (ParserConfigurationException e) {}
            }

    }

One of the most appalling design decisions in JAXP (and there were many) was that SAXParserFactory by default creates a parser that is not namespace-aware. JAXP中最令人震惊的设计决策之一(并且有很多)是SAXParserFactory默认创建的解析器不支持名称空间。 Always call setNamespaceAware(true) on the returned parser. 始终在返回的解析器上调用setNamespaceAware(true)。 Otherwise, the XMLReader will call startElement using the options defined for a non-namespace-aware parser, which means it will supply the lexical QName, but not the local-name and URI. 否则,XMLReader将使用为非命名空间感知的解析器定义的选项来调用startElement,这意味着它将提供词法QName,但不提供本地名称和URI。

The qName parameter contains the element name. qName参数包含元素名称。

Here's an example although it's hard to read because of formatting. 这是一个示例,尽管由于格式难于阅读。

Namespacing etc. makes a difference in where/how to get at element names. 名称空间等在元素名称的获取位置/方式上有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM