到目前为止,这是一个失败的故事,欢迎提出任何建议。我尝试了以下方法:
XMLStreamReader to Document 使用 JAXP 和 Saxon
编码:
JSONObject obj = new JSONObject("{\"alice\":{\"bob\": \"a\"}}");
AbstractXMLStreamReader reader = new MappedXMLStreamReader(obj);
StAXSource source = new StAXSource(reader);
Document document = XMLUtils.createDocument();
DOMResult result = new DOMResult(document);
Transformer identity = TransformerUtils.getIdentityTransformer();
identity.transform(source, result);
result.getNode();
这失败了,因为我们使用的 Saxon 转换器不支持 StAXSource。例外是:
org.orbeon.saxon.trans.DynamicError: A source of type javax.xml.transform.stax.StAXSource is not supported in this environment
org.orbeon.saxon.event.Sender.send(Sender.java:185)
org.orbeon.saxon.IdentityTransformer.transform(IdentityTransformer.java:29)
org.orbeon.oxf.xml.TransformerWrapper.transform(TransformerUtils.java:597)
org.apache.jsp.gaga_jsp._jspService(gaga_jsp.java:93)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
XMLStreamReader to Document 使用 JAXP 和 Xalan
编码:
JSONObject obj = new JSONObject("{\"alice\":{\"bob\": \"a\"}}");
AbstractXMLStreamReader reader = new MappedXMLStreamReader(obj);
StAXSource source = new StAXSource(reader);
Document document = XMLUtils.createDocument();
DOMResult result = new DOMResult(document);
Transformer identity = orbeon.apache.xalan.processor.TransformerFactoryImpl.newInstance().newTransformer();
identity.transform(source, result);
result.getNode();
Xalan 2.5.1(我们正在使用的版本)失败了。最新版本是 2.7.1,也许他们在该版本中添加了对 StAXSource 的支持。
javax.xml.transform.TransformerException: Can't transform a Source of type javax.xml.transform.stax.StAXSource
orbeon.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:372)
org.apache.jsp.gaga_jsp._jspService(gaga_jsp.java:82)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
使用 XMLEventWriter 和 DOMResult 的 XMLStreamReader 到文档
编码:
JSONObject obj = new JSONObject("{\"alice\":{\"bob\": \"a\"}}");
AbstractXMLStreamReader streamReader = new MappedXMLStreamReader(obj);
XMLInputFactory readerFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = readerFactory.createXMLEventReader(streamReader);
Document document = XMLUtils.createDocument();
DOMResult result = new DOMResult(document);
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(result);
eventWriter.add(eventReader);
eventWriter.close();
result.getNode();
这导致了 Xerces 中的 NPE:
java.lang.NullPointerException
orbeon.apache.xerces.dom.CoreDocumentImpl.setXmlVersion(CoreDocumentImpl.java:850)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.sun.xml.internal.stream.writers.XMLDOMWriterImpl.writeStartDocument(XMLDOMWriterImpl.java:583)
com.sun.xml.internal.stream.writers.XMLEventWriterImpl.add(XMLEventWriterImpl.java:96)
com.sun.xml.internal.stream.writers.XMLEventWriterImpl.add(XMLEventWriterImpl.java:72)
org.apache.jsp.gaga_jsp._jspService(gaga_jsp.java:95)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
使用 XMLEventWriter 和 Writer 的 XMLStreamReader 到文档
编码:
JSONObject obj = new JSONObject("{\"alice\": {\"bob\": \"a\"}}");
AbstractXMLStreamReader streamReader = new MappedXMLStreamReader(obj);
XMLInputFactory readerFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = readerFactory.createXMLEventReader(streamReader);
StringWriter stringWriter = new StringWriter();
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(stringWriter);
eventWriter.add(eventReader);
eventWriter.close();
out.print(stringWriter.toString());
使用 Java 5,它会寻找com.bea.xml.stream.MXParserFactory
无处可寻的:
javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.MXParserFactory not found
javax.xml.stream.FactoryFinder.newInstance(FactoryFinder.java:72)
javax.xml.stream.FactoryFinder.find(FactoryFinder.java:178)
javax.xml.stream.FactoryFinder.find(FactoryFinder.java:92)
javax.xml.stream.XMLInputFactory.newInstance(XMLInputFactory.java:136)
org.apache.jsp.gaga_jsp._jspService(gaga_jsp.java:79)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
使用 Java 6,它会导致 Sun 的 NPE XMLEventAllocatorImpl
:
java.lang.NullPointerException
com.sun.xml.internal.stream.events.XMLEventAllocatorImpl.getXMLEvent(XMLEventAllocatorImpl.java:82)
com.sun.xml.internal.stream.events.XMLEventAllocatorImpl.allocate(XMLEventAllocatorImpl.java:55)
com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(XMLEventReaderImpl.java:86)
com.sun.xml.internal.stream.writers.XMLEventWriterImpl.add(XMLEventWriterImpl.java:72)
org.apache.jsp.gaga_jsp._jspService(gaga_jsp.java:85)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
使用 BadgerFishDOMDocumentParser
代码:
ByteArrayInputStream bais = new ByteArrayInputStream("{\"alice\": {\"bob\": \"a\"}}".getBytes());
Document resDOM = new BadgerFishDOMDocumentParser().parse(bais);
这为我们提供了 Sun 的 NPE XMLEventAllocatorImpl
:
java.lang.NullPointerException
com.sun.xml.internal.stream.events.XMLEventAllocatorImpl.getXMLEvent(XMLEventAllocatorImpl.java:82)
com.sun.xml.internal.stream.events.XMLEventAllocatorImpl.allocate(XMLEventAllocatorImpl.java:55)
com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(XMLEventReaderImpl.java:86)
com.sun.xml.internal.stream.writers.XMLEventWriterImpl.add(XMLEventWriterImpl.java:72)
org.codehaus.jettison.AbstractDOMDocumentParser.parse(AbstractDOMDocumentParser.java:66)
org.apache.jsp.gaga_jsp._jspService(gaga_jsp.java:79)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)