0

我正在尝试从 odt 文件(使用 LibreOffice 创建)中读取数据。要求是获取绑定到文档中包含的 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)。

4

1 回答 1

1

我已经设法通过 jdom (odfdom-java) 做到这一点,毕竟没有使用。绑定的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);
    }
}
于 2015-08-27T16:06:30.813 回答