2015-08-27 128 views
0

我正在嘗試從odt文件(使用LibreOffice創建)讀取數據。要求是獲取綁定到文檔中包含的XForm的xml。我目前使用odfdom-java庫來讀取文件。到目前爲止,我已經設法通過用jdom解析文檔來讀取表單域的值,但我真正想要的是使用表單數據獲取整個xml。或者,我可以加載文件爲使用java從odt文件讀取XForm

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)。

回答

0

我已經設法通過jdom(odfdom-java)來做到這一點,畢竟沒有使用。綁定的xml存在於表示odt的xml中。您所需要的只是瞭解表格的標識或標籤的名稱,以獲得正確的節點。之後,構造一個包含帶表單數據的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); 
    } 
}