将Java w3c文档转换为XMLStreamReader [英] Convert Java w3c Document to XMLStreamReader

查看:204
本文介绍了将Java w3c文档转换为XMLStreamReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我们的代码库中重复使用一些接受XMLStreamReader的现有代码,我的应用程序将所需的数据作为w3c文档.

I would like to reuse some existing code in our code base that accepts an XMLStreamReader my application has the required data as a w3c Document.

以下示例是最小测试用例:

The following example is a minimum test case:

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();

    Document doc = builder.newDocument();

    Element rootElement = doc.createElement("Groups");
    doc.appendChild(rootElement);
    Element group = doc.createElement("Group");
    group.setTextContent("Wibble");
    rootElement.appendChild(group);

    DOMSource source = new DOMSource(doc);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(source);

    reader.nextTag();
    System.out.println("NextTag:" + reader.getName());
}

预期的输出应类似于:NextTag:Groups,但将引发以下内容:

The expected output should be something like: NextTag:Groups but instead the following is thrown:

Exception in thread "main" javax.xml.stream.XMLStreamException: java.net.MalformedURLException
    at com.sun.xml.stream.XMLReaderImpl.setInputSource(XMLReaderImpl.java:196)
    at com.sun.xml.stream.XMLReaderImpl.<init>(XMLReaderImpl.java:179)
    at com.sun.xml.stream.ZephyrParserFactory.createXMLStreamReader(ZephyrParserFactory.java:139)
    at Main.main(Main.java:27)
    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:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.net.MalformedURLException
    at java.net.URL.<init>(URL.java:601)
    at java.net.URL.<init>(URL.java:464)
    at java.net.URL.<init>(URL.java:413)
    at com.sun.xml.stream.XMLEntityManager.startEntity(XMLEntityManager.java:762)
    at com.sun.xml.stream.XMLEntityManager.startDocumentEntity(XMLEntityManager.java:697)
    at com.sun.xml.stream.XMLDocumentScannerImpl.setInputSource(XMLDocumentScannerImpl.java:300)
    at com.sun.xml.stream.XMLReaderImpl.setInputSource(XMLReaderImpl.java:193)
    ... 8 

当前使用Java 6 update 22.

Currently using Java 6 update 22.

更多信息:

Further info: The source to ZephyrParserFactory#jaxpSourcetoXMLInputSource seems to indicate that the Source object is converted by coping it's SystemId rather than the actual contents of the DOMSource.

更新:上面的原始测试用例实际上是使用我的项目类路径运行的,该项目类路径实际上包括JAXB 2.2.1库,而该库又引入了sjsxp 1.0.1.在干净的类路径上运行会产生:

Update: My orignal test case above was actually run using my project classpath which actually includes the JAXB 2.2.1 library which in turn pulls in sjsxp 1.0.1. Running on a clean classpath yields:

Exception in thread "main" java.lang.UnsupportedOperationException: Cannot create XMLStreamReader or XMLEventReader from a javax.xml.transform.dom.DOMSource
    at com.sun.xml.internal.stream.XMLInputFactoryImpl.jaxpSourcetoXMLInputSource(XMLInputFactoryImpl.java:302)
    at com.sun.xml.internal.stream.XMLInputFactoryImpl.createXMLStreamReader(XMLInputFactoryImpl.java:145)

符合@Gary Rowe的答案.

Which fits with @Gary Rowe's answer.

推荐答案

有些复杂,但是任何支持XQJ API的XQuery实现(例如Saxon)都将允许您提供DOM作为查询的输入". ",并以XMLStreamReader的形式获取结果.尽管涉及很多重量级机器,但它应该是非常有效的.

It's somewhat convoluted, but any XQuery implementation that supports the XQJ API (for example Saxon) will allow you to supply a DOM as the input to the query ".", and get the result as an XMLStreamReader. Although there's a lot of heavyweight machinery involved, it should be perfectly efficient.

使用Saxon,您还可以使用类似的东西将XQuery端短路

With Saxon you could also short-circuit the XQuery side of things using something like

Document doc; // the DOM document
XMLStreamReader reader = new PullToStax(PullProvider.makePullProvider(new DocumentWrapper(doc));

但是我认为XQJ方法更干净.

but I think the XQJ approach is cleaner.

这篇关于将Java w3c文档转换为XMLStreamReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆