gpt4 book ai didi

java - 使用 ROME 解析 RSS 提要时出现 MalformedByteSequenceException

转载 作者:行者123 更新时间:2023-11-30 06:31:36 25 4
gpt4 key购买 nike

我在运行使用 Rome Rss-parsing Lib 的 JSF 应用程序时遇到 MalformedByteSequenceException:

[2017-08-31T15:54:01.241+0200] [glassfish 5.0] [SEVERE] [] [] [tid: _ThreadID=101 _ThreadName=Thread-9] [timeMillis: 1504187641241] [levelValue: 1000] [[
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Ungültiges Byte 1 von 1-Byte-UTF-8-Sequenz.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:701)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:567)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1793)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(XMLEntityScanner.java:1463)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2824)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
at main.java.tools.RssAggregator.searchAggregatedFeeds(RssAggregator.java:119)
at main.java.jobs.ApplicationRunnable.lambda$execute$0(ApplicationRunnable.java:45)
at java.lang.Iterable.forEach(Iterable.java:75)
at main.java.jobs.ApplicationRunnable.execute(ApplicationRunnable.java:43)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)
]]

错误在 RssAggregator.searchAggreatedFeeds() 第 119 行抛出:

public List<Feed> searchAggregatedFeeds(String keyword) {
keyword = keyword.toLowerCase();
List<Feed> results = new LinkedList<>();
Document document = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

//document = builder.parse(IOUtils.toInputStream(this.aggregatedRssString);
InputStream stream = new ByteArrayInputStream(this.aggregatedRssString.getBytes());
document = builder.parse(stream); //TODO HERE We have the Problem
stream.close();

//Normalize the XML Structure; It's just too important !!
document.getDocumentElement().normalize();

} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}

//Here comes the root node
if (document != null) {

//Element root = document.getDocumentElement();
//Get all
NodeList nList = document.getElementsByTagName("item");

for (int i = 0; i < nList.getLength(); i++) {

Node node = nList.item(i);
Element ele = (Element) node;

Feed feed = new Feed();
feed.setDate(ele.getElementsByTagName("dc:date").item(0).getTextContent());
feed.setDescription(ele.getElementsByTagName("description").item(0).getTextContent());
feed.setLink(ele.getElementsByTagName("link").item(0).getTextContent());
feed.setSubject(ele.getElementsByTagName("title").item(0).getTextContent());
feed.setId(getIdCounter()); //set the id. This increments also

/* now the searching ... */
if (feed.getSubject().toLowerCase().contains(keyword) || feed.getDescription().toLowerCase().contains(keyword))
results.add(feed);
}
}
return results;
}

我不知道 document = builder.parse(stream); 行有什么问题。//TODO HERE 我们有问题

令我印象深刻的是,当使用 junit-test 进行测试时,一切都很顺利:

@Test
public void searchTest() throws Exception {
List<Feed> results = rssAggregator.searchAggregatedFeeds("a");
System.out.println("Hits: " + results.size());

List<Feed> results2 = rssAggregator.searchAggregatedFeeds("z");
System.out.println("Hits: " + results2.size());

List<Feed> results3 = rssAggregator.searchAggregatedFeeds("x");
System.out.println("Hits: " + results3.size());

}

结果如预期: enter image description here

...从循环作业调用它时不会:

    List<Feed> allResults = new LinkedList<>();

/* for each WORD search the aggregatedfeed for matches and add them to allResults */
configBean.words.forEach(w -> {
System.out.println("current searched for Word: " + w);
List<Feed> wResults = aggregator.searchAggregatedFeeds(w); // :(
allResults.addAll(wResults);
});

问题:为什么 junit-test 可以工作,而应用程序内部的调用却不能?聚合的 Feed 完全相同。

最佳答案

您的 rss 数据是否采用 UTF-8 编码?调用 String.getBytes() 的无参数版本将使用平台的默认字符集,并且可能不是 UTF-8。这就是为什么您的测试可能在 Linux 上有效但在 Windows 上失败。尝试使用 String.getBytes(Charset charset) 代替。

关于java - 使用 ROME 解析 RSS 提要时出现 MalformedByteSequenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45983720/

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