无法从XML获取描述

时间:2015-11-27 12:18:31

标签: java xml unicode xml-parsing

我使用了以下代码。我对其他标签没有问题,但我无法获得描述内容。我认为新闻提要的字符集可能存在问题。有土耳其字符,他们可能需要使用unicode阅读器处理。但我不知道如何实现这一目标。

public class NewsFeed {

final URL url;
static final String TITLE = "title";
static final String DESCRIPTION = "description";
static final String CHANNEL = "channel";
static final String LANGUAGE = "language";
static final String COPYRIGHT = "copyright";
static final String LINK = "link";
static final String AUTHOR = "author";
static final String ITEM = "item";
static final String PUB_DATE = "pubDate";
static final String GUID = "guid";
static final String IMAGE = "image";

NewsFeed() throws XMLStreamException, MalformedURLException {

    url = new URL("http://www.haberturk.com/rss/manset.xml");
    boolean isFeedHeader = true;
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    InputStream in = read();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);

    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();

        if (event.isStartElement()) {
            String localPart = event.asStartElement().getName()
                    .getLocalPart();

            switch (localPart) {
                case ITEM:
                    if (isFeedHeader) {
                        isFeedHeader = false;
                    }
                    event = eventReader.nextEvent();
                    break;
                case TITLE:
                    String title = getCharacterData(event, eventReader);
                    System.out.println(title + "////");
                    break;
                case DESCRIPTION:
                    String description = getCharacterData(event, eventReader);
                    System.out.println(description);
                    break;
                case LINK:
                    String link = getCharacterData(event, eventReader);
                    System.out.println(link);

            }
        }

    }
}

private InputStream read() {
    try {
        return url.openStream();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

private String getCharacterData(XMLEvent event, XMLEventReader eventReader)
        throws XMLStreamException {
    String result = "";
    event = eventReader.nextEvent();
    if (event instanceof Characters) {
        result = event.asCharacters().getData();
    }
    return result;
}

public static void main(String[] args) throws MalformedURLException, XMLStreamException {
    new NewsFeed();
}

}

this is output

2 个答案:

答案 0 :(得分:0)

为什么在while块中你有:event = eventReader.nextEvent();

while () {
    event = eventReader.nextEvent();
    .
    .
    .
}

你还有另一个event = eventReader.nextEvent()的调用; 在getCharacterData方法中。

event = eventReader.nextEvent();
if (event instanceof Characters) {
    result = event.asCharacters().getData();
}

可能这就是问题所在!

希望这有帮助!

答案 1 :(得分:0)

试试这个:

        URL url = new URL("myURL");
        InputStream is=url.openStream();
        BufferedReader brd = new BufferedReader(new InputStreamReader(is, "UTF-8"));

        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader eventReader = factory.createXMLEventReader(brd);