gpt4 book ai didi

java - 使用java从odt文件读取XForm

转载 作者:行者123 更新时间:2023-12-01 11:11:57 32 4
gpt4 key购买 nike

我正在尝试从 odt 文件(使用 LibreOffice 创建)读取数据。要求是获取绑定(bind)到文档中包含的 XForm 的 xml。我目前正在使用 odfdom-java 库来读取文件。到目前为止,我已经通过使用 jdom 解析文档来设法读取表单字段的值,但我真正想要的是获取包含表单数据的整个 xml。或者,我可以将文件加载为

OdfTextDocument.loadDocument("C://myFile.odt");

有谁知道如何从那里获取 XForm xml?

或者,如果我以编程方式将 odt 文件转换为 pdf 会有帮助吗?使用pdfbox我已经成功获得了acroform

    PDDocument pdDoc = PDDocument.loadNonSeq( new File("C://myFile.odt"), null);
PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
PDAcroForm pdAcroForm = pdCatalog.getAcroForm();

但之后遇到同样的问题(如何获取带有表单数据的xml)。

最佳答案

我已经设法通过jdom(odfdom-java)做到这一点,毕竟没有使用。绑定(bind)的 xml 本身存在于表示 odt 的 xml 中。您所需要的只是知道表单的 id 或标签的名称,以便获得正确的节点。然后,构建一个包含带有表单数据的 xml 的字符串。我的代码如下:

import org.apache.xerces.dom.DeepNodeListImpl;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class TestXFormData {

private static StringBuilder nodeContent;

public static void main(String[] args) throws Exception {
//Unzip the openOffice Document
ZipFile zipFile = new ZipFile("C://myFile.odt");
Enumeration entries = zipFile.entries();
ZipEntry entry;

while(entries.hasMoreElements()) {
entry = (ZipEntry) entries.nextElement();
if (entry.getName().equals("content.xml")) {
// construct document
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = domFactory.newDocumentBuilder();
Document doc = docBuilder.parse(zipFile.getInputStream(entry));
// print the document
printDocument(doc);
// get the node
NodeList list = doc.getElementsByTagName("myTagName");
Node node = ((DeepNodeListImpl) list).item(0);
nodeContent = new StringBuilder();
// print the xml with the form data
prettyPrint(node);
System.out.println(nodeContent.toString());
}
}
}


private static void prettyPrint(Node node) {
if (node.getNodeType() == Node.TEXT_NODE) {
nodeContent.append(node.getNodeValue());
} else if (node.getNodeType() == Node.ELEMENT_NODE) {
nodeContent.append("<" + node.getNodeName() + ">");
NodeList kids = node.getChildNodes();
for (int i = 0; i < kids.getLength(); i++) {
prettyPrint(kids.item(i));
}
nodeContent.append("</" + node.getNodeName() + ">");
}
}


private static void printDocument(Document doc) throws IOException {
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
XMLSerializer serializer = new XMLSerializer(System.out, format);
serializer.serialize(doc);
}
}

关于java - 使用java从odt文件读取XForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32245500/

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