Solved

Using Jettison to go from JSON to DOM

Posted on 2009-05-11
9
1,231 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

910 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

25 Experts available now in Live!

Get 1:1 Help Now