Solved

Using Jettison to go from JSON to DOM

Posted on 2009-05-11
9
1,235 Views
Last Modified: 2013-11-08
The Jettison's User Guide provides an example on how to go from JSON to XML using the STaX API (http://jettison.codehaus.org/User%27s+Guide), but it doesn't say anything about the DOM API.

I read that Jettison 1.1 supports the DOM API. Does anyone have an example for this? I.e how to convert JSON to a W3C DOM object?

Alex
0
Comment
Question by:avernet
  • 5
  • 4
9 Comments
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 24361720
Even if they claim to have a DOM API it seems it is not really finished and even less documented. I have however found a reference to somebody who implemented a Dom parsing/serialzing feature. Maybe this helps you:
http://archive.jettison.codehaus.org/lists/dev@jettison.codehaus.org/msg/47DFE101.5030005@jboss.com
0
 
LVL 1

Author Comment

by:avernet
ID: 24368223
@Christofer,

Thank you for the link to this post. I had not tried BadgerFishDOMDocumentParser. Now I did, but get an NPE in Sun's XMLEventAllocatorImpl. I put more information on what I tried so far on the page below. You will see the code using BadgerFishDOMDocumentParser last on that page:

http://stackoverflow.com/questions/854344/using-jettison-to-go-from-json-to-dom-in-java

Alex
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 24369002
Hi avernet,

I think the first approach sounds the most promissing one. You write that you are using Saxon and that Saxon doesn't impelment the STaX input. Well how about using Xalan instead?

With the following code you should be able to instantiate an instance, that you could use to create your identityTransformer (Which is nothing else than Input Source directly connected to the output source.

org.apache.xalan.xslt.XSLTProcessor processor =  org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor(  );
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 1

Author Comment

by:avernet
ID: 24370673
@ChristoferDutz,

Good idea. But at least with the version of Xalan we are using (2.5.1, and yes, I know there is a 2.7.1), I'm getting the following exception. Maybe support for StAXSource was added in a more recent version.

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)


0
 
LVL 1

Author Comment

by:avernet
ID: 24370697
I also added the code I test using the Xalan transformer, as well as the exception I am getting to:

http://stackoverflow.com/questions/854344/using-jettison-to-go-from-json-to-dom-in-java
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 24372116
Mabe you should look at: http://woodstox.codehaus.org/ 
I have read, that some people use this to build Dom Objects from STaX
It seems to be compatible with jettison:
http://jettison.codehaus.org/dependencies.html
0
 
LVL 1

Author Comment

by:avernet
ID: 24388059
@ChristoferDutz,

I am not familiar with Woodstox. Are you saying that I can get a JAXP transformer implemented by Woodstox instead of using the one from Xalan (i.e. instead of org.apache.xalan.processor.TransformerFactoryImpl.newInstance().newTransformer())?
0
 
LVL 20

Accepted Solution

by:
ChristoferDutz earned 125 total points
ID: 24421075
I haven't used Woodstox either. I just read that it's compatible with parsing STaX and generating from these events. So I just wanted to point you the driection without having been there myself ;-)
0
 
LVL 1

Author Comment

by:avernet
ID: 24424138
ChristoferDutz,

I got around this by sending XML from the browser to the server instead of JSON, so I lack the energy right not to try more things ;). But I think your last comment here will definitely steer people in the right direction. Thanks!

Alex
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question