Title: Node.js Example to convert Xml to JSON for large Xml file
Tags: json;node.js;xml-parsing
Question: I'm relatively new to Node.js. I'm trying to convert 83 XML files that are each around 400MB in size into JSON.
Each file contains data like this (except each element has a large number of additional statements):
<case-file&gt;
<serial-number&gt;75563140</serial-number&gt;
<registration-number&gt;0000000</registration-number&gt;
<transaction-date&gt;20130101</transaction-date&gt;
<case-file-header&gt;
<filing-date&gt;19981002</filing-date&gt;
<status-code&gt;686</status-code&gt;
<status-date&gt;20130101</status-date&gt;
</case-file-header&gt;
<case-file-statements&gt;
<case-file-statement&gt;
<type-code&gt;D10000</type-code&gt;
<text&gt;"MUSIC"</text&gt;
</case-file-statement&gt;
<case-file-statement&gt;
<type-code&gt;GS0351</type-code&gt;
<text&gt;compact discs</text&gt;
</case-file-statement&gt;
</case-file-statements&gt;
<case-file-event-statements&gt;
<case-file-event-statement&gt;
<code&gt;PUBO</code&gt;
<type&gt;A</type&gt;
<description-text&gt;PUBLISHED FOR OPPOSITION</description-text&gt;
<date&gt;20130101</date&gt;
<number&gt;28</number&gt;
</case-file-event-statement&gt;
<case-file-event-statement&gt;
<code&gt;NPUB</code&gt;
<type&gt;O</type&gt;
<description-text&gt;NOTICE OF PUBLICATION</description-text&gt;
<date&gt;20121212</date&gt;
<number&gt;27</number&gt;
</case-file-event-statement&gt;
</case-file-event-statements&gt;
I have tried a lot of different Node modules, including sax, node-xml, node-expat and xml2json. Obviously, I need to stream the data from the file and pipe it through an XML parser and then convert it to JSON.
I have also tried reading a number of blogs, etc. attempting to explain, albeit superficially, how to parse Xml.
In the Node universe, I tried sax first but I can't figure out how to extract the data in a format that I can convert it to JSON. xml2json won't work on streams. node-xml looks encouraging but I can't figure out how it parses chunks in any manner that makes sense. node-expat points to libexpat documentation, which appears to requires a Ph.D. Node elementree does the same, pointing to the Python implementation but doesn't explain what has been implemented or how to use it.
Can someone point me to example that I could use to get started?
Comment: u can use node module jquery to parse xml (same as browser side).

Comment: Parsing the xml isn't the problem. I've had success with sax. It streams the data quite well. The problem is converting it to Json. I couldn't find a streaming Json parser. I didn't try node jquery because its readme says that it doesn't work on windows and I'm developing on windows. I finally ended up writing a Json converter.


Here is another answer: Although this question is quite old, I am sharing my problem &amp; solution which might be helpful to all who are trying to convert XML to JSON.
The actual problem here is not the conversion but processing huge XML files without having to hold them in roylawrence@example.org.
Working with almost all widely used packages, I came across following problem -
A lot of packages support XML to JSON conversion covering all scenarios but they don't work well with large files.
Very few packages (like xml-flow, xml-stream) support large XML file conversion but the conversion process misses out few corner case scenarios where the conversion either fails or gives unpredictable JSON structure (explained in this SO question).
The ideal solution would be to combine the advantages from both the approaches which is exactly what I did and came up with xtreamer node package.
In simple words, xtreamer accepts repeating node just like xml-flow / xml-stream but emits repeating xml nodes instead of converted JSON. This provides following advantages -
We can pipe xtreamer with any readable stream as it extends transform stream.
The emitted XML nodes can be transferred to any XML to JSON parser to get desired JSON.
We can go one step further and hook up the JSON parser with xtreamer &amp; it will invoke the JSON parser and emit JSON accordingly.
xtreamer has stream as its only dependency &amp; being a transform stream extension, it can be piped with other streams flexibly.
What if XML structure is not fixed?
I managed to come up with another sax based node package xtagger which reads the XML file and provides the structure of the file in following format -
structure: { [name: string]: { [hierarchy: number]: number } };
This package allows to figure out the repeating node name which can then be passed to xtreamer for parsing.
I hope this helps. :)

Here is another answer: I guess that by now you have a working process, considering your last answer.
Anyway, if you've already successfully parsed the incoming data with SAX, the solution might simply put the data in an object of your design and use   yourStream.write(JSON.stringify(yourObject))
to stream it out.

Here is another answer: I doubt this is still relevant after 2-3 years but in case anyone else stumbles on this, I would say xml-stream on NPM looked rather straightforward to me.
If you're a windows user who wants to avoid GYP however I tried adding a very simple solution using sax to extract children form an XML file one by one, it's called no-gyp-xml-stream and it may not have a lot of features, but it certainly is simple to use: https://www.npmjs.com/package/no-gyp-xml-stream

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:02/13/13', 'FavoriteCount': 3.0, 'PostTypeId': 1, 'Tags': '<json><node.js><xml-parsing>', 'Title': 'Node.js Example to convert Xml to JSON for large Xml file', 'ViewCount': 5139, 'id': 14845922, 'user_AboutMe': '', 'user_AccountId': 1096787.0, 'user_DisplayName': 'rob_hicks', 'user_Reputation': 1724.0, 'user_Views': 117.0, 'user_id': 1090221.0}}