【问题标题】:Java stax: The reference to entity "R" must end with the ';' delimiterJava stax:对实体“R”的引用必须以“;”结尾分隔符
【发布时间】:2020-07-14 10:43:24
【问题描述】:

我正在尝试使用 stax 解析 xml,但我得到的错误是:

javax.xml.stream.XMLStreamException: ParseError at [row,col]:[414,47]
Message: The reference to entity "R" must end with the ';' delimiter.

卡在 xml 文件中 P&Rinside 的第 414 行。我必须解析的代码是:

public List<Vild> getVildData(File file){
    XMLInputFactory factory = XMLInputFactory.newFactory();
    try {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(Files.readAllBytes(file.toPath()));
        XMLStreamReader reader = factory.createXMLStreamReader(byteArrayInputStream, "iso8859-1");
        List<Vild> vild = saveVild(reader);
        reader.close();
        return vild;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    return Collections.emptyList();
}
private List<Vild> saveVild(XMLStreamReader streamReader) {
    List<Vild> vildList = new ArrayList<>();
    try{
        Vild vild = new Vild();
        while (streamReader.hasNext()) {
            streamReader.next();
            //Creating list with data
        }
    }catch(XMLStreamException | IllegalStateException ex) {
        ex.printStackTrace();
    }
    return Collections.emptyList();
}

我在网上看到&amp; 是无效的 xml 代码,但我不知道如何在它在 saveVild 方法中引发此错误之前更改它。有人知道如何有效地做到这一点吗?

【问题讨论】:

    标签: java xml xml-parsing stax


    【解决方案1】:

    换个问题:您不是在尝试解析 XML 文件,而是在尝试解析非 XML 文件。为此,您需要一个非 XML 解析器,并且要编写这样一个解析器,您需要从您尝试解析的语言的规范开始,并且您需要与其他合作伙伴就该语言的规范达成一致数据交换。

    遵守标准可以节省多少工作量!

    像对待来自供应商的任何其他损坏货物一样对待到达您商店的损坏的 XML:将其退回给标有“不适合用途”的发件人。

    【讨论】:

      猜你喜欢
      • 2011-07-05
      • 2011-09-22
      • 2014-05-26
      • 2010-12-29
      • 1970-01-01
      • 2012-12-16
      • 2019-10-04
      • 2015-10-19
      相关资源
      最近更新 更多