gpt4 book ai didi

java - 使用 Transformer 处理空 CDATA 时出现 IndexOutOfBoundsException

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:21:02 25 4
gpt4 key购买 nike

我想从大型 XML 文件中提取特定节点。这很有效,直到出现没有任何内容的疯狂 CDATA。

输出:

ERROR:  ''
javax.xml.transform.TransformerException: java.lang.IndexOutOfBoundsException
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:732)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:336)
at xml_test.XML_Test.extractXML2(XML_Test.java:698)
at xml_test.XML_Test.main(XML_Test.java:811)
Caused by: java.lang.IndexOutOfBoundsException
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.getTextCharacters(XMLStreamReaderImpl.java:1143)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleCharacters(StAXStream2SAX.java:261)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(StAXStream2SAX.java:171)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(StAXStream2SAX.java:120)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:674)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:723)
... 3 more
---------
java.lang.IndexOutOfBoundsException
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.getTextCharacters(XMLStreamReaderImpl.java:1143)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleCharacters(StAXStream2SAX.java:261)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(StAXStream2SAX.java:171)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(StAXStream2SAX.java:120)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:674)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:723)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:336)
at xml_test.XML_Test.extractXML2(XML_Test.java:698)
at xml_test.XML_Test.main(XML_Test.java:811)

代码:

InputStream stream = new FileInputStream("C:\\myFile.xml");
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(stream);

TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();

String extractPath = "/root";
String path = "";

while(reader.hasNext()) {
reader.next();

if(reader.isStartElement()) {
path += "/" + reader.getLocalName();

if(path.equals(extractPath)) {
StringWriter writer = new StringWriter();
StAXSource src = new StAXSource(reader);
StreamResult res = new StreamResult(writer);
t.transform(src, res); // Exception thrown

System.out.println(writer.toString());

path = path.substring(0, path.lastIndexOf("/"));
}
}
else if(reader.isEndElement()) {
path = path.substring(0, path.lastIndexOf("/"));
}
}

引发错误的 XML:

<foo><![CDATA[]]></foo>

我可以让 Transformer 忽略它吗?或者另一个实现会是什么样子?我无法更改输入 XML!

最佳答案

这是关于 Xerces 实现的问题,请检查: https://issues.apache.org/jira/browse/XERCESJ-1033

似乎空的 CDATA 不应该存在,所以我能给你的唯一建议是:

  1. 更改 XML 解析器实现
  2. 从源文件中删除空 CDATA(将“<![CDATA[]]>”替换为“”)
    或者在 CDATA 中放置一个空格,例如<![CDATA[ ]]>

我添加了一些其他实现的示例。

贾克斯

在 Jaxb 中,您可以通过一种简单的方式将 XML 映射到 POJO。

例如,如果您在 c:\myFile.xml 中有下一个 xml 文件:

<root>
<foo><![CDATA[]]></foo>
<foo><![CDATA[some data here]]></foo>
</root>

您可以拥有下一个 POJO:

@XmlRootElement
public class Root {

@XmlElement(name="foo")
privateList<Foo> foo;

public List<Foo> getFooList() {
return foo;
}

public void setFooList(List<Foo> fooList) {
this.foo = fooList;
}

}

@XmlType(name = "foo")
public class Foo {

@XmlValue
private String content;

@Override
public String toString() {
return content;
}

}

然后使用下一个片段从 XML 解析为对象:

    public static void main(String[] args) {
try {

File file = new File("C:\\myFile.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Root root = (Root) jaxbUnmarshaller.unmarshal(file);

for (Foo foo : root.getFooList()) {
System.out.println(String.format("Foo content: |%s|", foo));
}

} catch (JAXBException e) {
e.printStackTrace();
}

}

我对此进行了测试,没有引发任何错误。

关于java - 使用 Transformer 处理空 CDATA 时出现 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28028174/

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