Solved

Using Jettison to go from JSON to DOM

Posted on 2009-05-11
9
1,230 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now