gpt4 book ai didi

java - SAX 解析器由于
标记而忽略文本

转载 作者:行者123 更新时间:2023-12-01 13:10:51 25 4
gpt4 key购买 nike

这里有一个小问题,我不知道如何解决。我有一个如下所示的 XML 文件:

<?xml version="1.0"?>
<item>
<title>Item 1</name>
<description>Description Text 1&lt;br /&gt;Description Text 2</description>
</item>

我有一个如下所示的 SAX 解析器:

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if ("item".equals(qName)) {
currentItem = new Item();
} else if ("title".equals(qName)) {
parsingTitle = true;
} else if ("description".equals(qName)) {
parsingDescription = true;
}
}

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

System.out.println("Testing endelement");

if ("item".equals(qName)) {
Items.add(currentItem);
currentItem = null;
} else if ("title".equals(qName)) {
parsingTitle = false;
} else if ("description".equals(qName)) {
parsingDescription = false;
}
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {

System.out.println("writing");

if (parsingTitle) {
if (currentItem != null)
currentItem.setTitle(new String(ch, start, length));
} else if (parsingDescription) {
if (currentItem != null) {
currentItem.setDescription(new String(ch, start, length));
parsingDescription = false;
}
}

问题是 SAX 仅解析标记中文本的第一部分,直到“
”文本(即
标记)并忽略其余部分。如何让 SAX 解析器忽略“
”并解析其余的描述?

谢谢。

最佳答案

正如评论中提到的,您不能依赖 characters() 一次性提供元素的所有文本。我推荐这样的内容(查找代码中的注释,看看我修改了哪里),然后对标题进行类似的修改:

// buffer to hold description
private StringBuffer descriptionBuffer;
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if ("item".equals(qName)) {
currentItem = new Item();
} else if ("title".equals(qName)) {
parsingTitle = true;
} else if ("description".equals(qName)) {
parsingDescription = true;
// initialize buffer
descriptionBuffer = new StringBuffer();
}
}

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

System.out.println("Testing endelement");

if ("item".equals(qName)) {
Items.add(currentItem);
currentItem = null;
} else if ("title".equals(qName)) {
parsingTitle = false;
} else if ("description".equals(qName)) {
// Put contents of buffer into description
currentItem.setDescription(descriptionBuffer.toString());
descriptionBuffer = null;
parsingDescription = false;
}
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {

System.out.println("writing");

if (parsingTitle) {
if (currentItem != null)
currentItem.setTitle(new String(ch, start, length));
} else if (parsingDescription) {
// add to buffer
descriptionBuffer.append(ch, start, length);
}
}

关于java - SAX 解析器由于 <br/> 标记而忽略文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22883296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com