Solved

Using Jettison to go from JSON to DOM

Posted on 2009-05-11
9
1,238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
isNaN issue on the Table input text 2 42
convert Absolute path to relative path of FTP location 7 51
javascript duplicates 9 26
Slow Down an Animation 3 29
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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 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