java解析odt_java - 使用Java从odt文件读取XForm - 堆栈内存溢出

我设法通过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("");

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值