【问题标题】:SAX getting only the end of a content stringSAX 仅获取内容字符串的结尾
【发布时间】:2013-09-20 22:46:31
【问题描述】:

我需要从 标签中获取数据,但我的处理程序只获取标签内容的结尾(例如最后三个单词)。我不知道该怎么做,因为其他标签正在按预期处理,获取所有内容。*

我已经看到解析器忽略了一些标签,但我认为它不会发生,因为正如我所说,它获取内容但只是结束。

源 XML 托管在 -> http://djpaulonla.podomatic.com/archive/rss2.xml

拜托,有人能帮帮我吗??? 代码如下:

public class PodOMaticCustomHandler extends CustomHandler {

public PodOMaticCustomHandler(int quantityToFetch, String startTagValue,
        String endTagValue) {
    super(quantityToFetch, startTagValue, endTagValue);
}

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

@Override
public void endDocument() throws SAXException {
    super.endDocument();
    this.endDoc = true;
}

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

    if (this.podcast != null) {
        if (qName.equalsIgnoreCase("title")) {
            podcast.setTitle(this.value);
        } else if (qName.equalsIgnoreCase("pubDate")) {
            podcast.setPubDate(this.value);
        } else if (qName.equalsIgnoreCase("description")) {
            podcast.setContent(this.value);
        } else if (qName.equalsIgnoreCase("guid")) {
            this.podcast.setLink(value);
        }
    }

}

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

    if (this.startTagValue == null) {
        this.startTagValueFound = true;
    } else if (qName.equalsIgnoreCase("guid")
            && this.value.equalsIgnoreCase(this.startTagValue)) {
        this.startTagValueFound = true;
    }
    if (this.endTagValue != null) {
        if (qName.equalsIgnoreCase("guid")
                && this.value.equalsIgnoreCase(this.endTagValue)) {
            this.endDoc = true;
        }
    }
    if (!this.endDoc) {
        if (this.quantityToFetch != this.podcasts.size()) {
            if (this.startTagValueFound == true) {
                if (qName.equalsIgnoreCase("item")) {
                    this.podcast = new Podcast();
                } else if (qName.equalsIgnoreCase("enclosure")) {
                    this.podcast.setMedia(attributes.getValue("url"));
                    this.podcasts.add(podcast);
                }
            }
        } else {
            this.podcast = null;
        }
    }else{
        this.podcast = null;
      }
    }
  }

【问题讨论】:

  • 您遇到的是一个非常常见的 SAX 绊脚石,您需要以不同的方式实现 characters 方法。请参阅我的近距离投票中链接的问题的答案以获取解释和示例。
  • Sax parsing and encoding的可能重复
  • 谢谢=D 真的很有用

标签: xml-parsing sax podcast


【解决方案1】:

您不能依赖于对整个元素文本调用一次 characters 方法,它可能会被调用多次,每次只使用部分文本。

将调试日志语句添加到字符方法中,显示您正在设置的值,您将看到值被字符串的第一部分设置,然后被最后一部分覆盖。

答案是在 CharArrayWriter 或 StringBuilder 中缓冲从字符调用传入的文本。然后你必须在找到元素末尾时清除缓冲区。

下面是the Java tutorial on SAX 对 characters 方法的看法:

解析器不需要一次返回任何特定数量的字符。解析器一次可以返回从单个字符到数千个字符的任何内容,并且仍然是符合标准的实现。因此,如果您的应用程序需要处理它看到的字符,明智的做法是让 characters() 方法将字符累积在 java.lang.StringBuffer 中,并仅在您确定所有字符都已找到时才对它们进行操作。

【讨论】:

  • 谢谢伙计。它真的很有用,一切都按我的意愿工作:)
猜你喜欢
  • 1970-01-01
  • 2015-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多