Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a SaxParser that reads json and fires events so it looks like xml

Tags:

java

json

xml

sax

This would be great as it would allow my xml stuff to read json w/out any change except for the different sax parser.

like image 943
mP. Avatar asked Dec 07 '10 03:12

mP.


People also ask

Can an XML parser parse JSON?

JSON is Unlike XML BecauseXML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript function.

What is SAX in Java?

SAX (Simple API for XML) is an event-based parser for XML documents. Unlike a DOM parser, a SAX parser creates no parse tree.


2 Answers

If you meant, event-based parser then there are a couple of projects out there that do this:

  1. http://code.google.com/p/json-simple/

    Stoppable SAX-like interface for streaming input of JSON text

    This project has moved to https://github.com/fangyidong/json-simple

  2. http://jackson.codehaus.org/Tutorial

    Jackson Streaming API is similar to Stax API

    This project has moved to https://github.com/FasterXML/jackson-core

like image 180
Ryan Fernandes Avatar answered Oct 21 '22 07:10

Ryan Fernandes


I think it is a bad idea to try treat JSON as if it was XML (which is what you are essentially asking); however, Jettison does just this. It exposes JSON content via Stax API (javax.xml.stream). And if you truly want SAX, writing wrapper from Stax to SAX is trivial as well (but not the other way around).

I also think you might get better answers if you explained bit more what you are trying to achieve, beyond mechanisms you are hoping to use. For example, there are many data binding tools for both XML and JSON; and using such tools could hide lower level details much better than using abstraction meant for one to process the other.

like image 24
StaxMan Avatar answered Oct 21 '22 09:10

StaxMan