English 中文(简体)
默认 Handler 中的空空 uri 和本地化Name
原标题:Empty uri and localName in DefaultHandler

I am developing an RSS reader. When running this code, localName and uri empty. I am parsing a RSS feed. I am running the following code. The same code is working fine in another android project.

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

    String name;
    if (localName == "" ){
        name = qName;
    }
    else{
        name = localName;
    }

    if (name.equalsIgnoreCase("title")) {
        currentPost.setTitle(chars.toString());
    }
    if (name.equalsIgnoreCase("link")) {
        currentPost.setLink(chars.toString());
    }
    if (name.equalsIgnoreCase("content")
            && currentPost.getContent() == null) {
        currentPost.setContent(chars.toString());
    }
    if (name.equalsIgnoreCase("item")) {
        currentPost.setFeed(feed);
        Posts.Instance().add(currentPost);
        currentPost = new Post();
    }
}
问题回答

据API说

参数:

uri - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.

localName - The local name (without prefix), or the empty string if Namespace processing is not being performed.

.

The characters method can get called multiple times while inside a tag, especially if the element value contains whitespace.

在字符 () docs 中

The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.

因此,

When I write SAX parsers, I use a StringBuilder to append everything passed to characters():

public void characters (char ch[], int start, int length) {
    if (buf!=null) {
        for (int i=start; i<start+length; i++) {
            buf.append(ch[i]);
        }
    }
}

Then in endElement(), I take the contents of the StringBuilder 和 do something with it. That way, if the parser calls characters() several times, I don t miss anything.

Credit: https://stackoverflow.com/a/7182648/643500https://stackoverflow.com/a/2838338/643500

<强度 > 编辑:

阅读>http://sujitpal.blogspot.com/2008/07/rsss-feed-client-in-java.html





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签