gpt4 book ai didi

java - 多次调用 java SaxParser.parse(String, Handler) 合法吗?

转载 作者:行者123 更新时间:2023-11-30 09:51:39 25 4
gpt4 key购买 nike

我有一个 java SAX 解析器,我想在具有不同 XML 文件的同一个处理程序上多次调用它。我将它与 iText 结合使用来创建一个多页 PDF 文档,其中一些页面有一个 XML 标签映射,而其他页面有另一个。例如,

parser.parse("xmlFile1", handler);
parser.parse("xmlFile2", handler);

当我尝试这样做时,我得到了一个 java.lang.RuntimeException 抛出和以下堆栈跟踪:

DocumentException: java.lang.RuntimeException: The document is not open.
at com.lowagie.text.pdf.PdfWriter.getDirectContent(PdfWriter.java:695)
at com.lowagie.text.pdf.PdfDocument.newPage(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.carriageReturn(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.add(Unknown Source)
at com.lowagie.text.Document.add(Unknown Source)
at myClass.myCode(Unknown Source)
org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:345)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:223)
at myClass.myCode(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)

SaxParser.parse 方法是否隐式调用 document.close()?还是我的代码中有其他问题需要隔离和更正?

最佳答案

  • 重复使用同一个解析器是合法的(只要不是同时使用)
  • 解析器触发“endDocument”。这似乎关闭了此处的 iText 文档。但这不是由解析器完成的 - 这是来自您的处理程序的代码。

关于java - 多次调用 java SaxParser.parse(String, Handler) 合法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4586930/

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