3

Working with XML in C++ seems a bit of a pain and I'm looking at a way to output JSON.

I've stumbled on two different approaches:

A) XSLT transformations: http://controlfreak.net/xml-to-json-in-xslt-a-toolkit/

B) Dedicated library such as this: https://github.com/Cheedoong/xml2json

I'm new to programming so pursuing an avenue is a very time consuming process. If possible id like to get some feedback on what might be a more recommended approach before I start.

I'm pulling in about 5000 xml entities and outputting json with curl to another server

Any help or advice would be appreciated.

Jimmy
  • 109
  • 1
  • 10
  • 3
    Working with JSON in C++ also seems a bit of a pain, I would use javascript to work with JSON. You can use nodejs to run the code on a console. And if you are new to programming, javascript is a cool language to learn. What is your final goal? that's an important part of the question. – cauchi Apr 16 '15 at 15:04
  • 2
    This isn't a task for C++. This is a task for Python or Ruby or really any language but C++. – Sebastian Redl Apr 16 '15 at 15:05
  • @jbcolmenares My final goal is to take some xml nodes spat out from C++ (which does some fairly hefty computation) and get it into Elasticsearch as quickly and efficiently as possible. – Jimmy Apr 16 '15 at 15:06
  • @SebastianRedl Is your recommendation to leave the computation to call the C++ from Python to do the computation work and pipe it out to Python to convert it to json? – Jimmy Apr 16 '15 at 15:08
  • 1
    Use the library - that's why libraries are for, and that one comes with source so you can tweak it to your exact needs if necessary. But the ultimate approach is to modify the sending application to spit out JSON instead. – gbjbaanb Apr 16 '15 at 15:08
  • @gbjbaanb Thank you for the advice. I can't have any affect on the data unfortunately, I can only do a conversion when it comes in – Jimmy Apr 16 '15 at 15:10
  • 2
    Then you need to update the question a little: can you write a standalone app in any language or is it part of an existing C++ program? Is performance an issue? what languages do you (and colleagues) know as future maintenance is important. – gbjbaanb Apr 16 '15 at 15:19
  • @gbjbaanb I would prefer to keep it in C++ just to save installing a lot of software, but it depends, if the end result would be much cleaner if I used C++ for the grunt computation and output a stream to another piece of software such as Python or node.js to convert to json and curl then so be it. I'm after the best and simplest result. I am only familiar with a bit of Python but eager to learn anything. – Jimmy Apr 16 '15 at 15:23
  • If you are looking for a solution, language does not matter. If you already selected your language, have your approaches listed, and focused on an answer of A or B, you will try both and select the optimum one. Without knowing why XML handling in C++ is a pain for you, it will not be easy to answer. – Umut Kahramankaptan Apr 22 '15 at 13:16

1 Answers1

6

From the comments... if everything else you have is C++ then the best answer is to write it in C++, building a mish-mash of different bits of programming languages is a right PitA to maintain and support.

So, if you have C++ and need to resolve XML to JSON, it seems obvious to use the xml2json library that you linked to. It comes with sources so you can modify it to suit any needs that it doesn't cater for, and should be the simplest answer to drop it into an existing component. You already have curl so this removes the need to come up with a different networking solution too.

gbjbaanb
  • 48,354
  • 6
  • 102
  • 172
  • I appreciate the answer. The XSLT solution also allows me to use C++, so I see it as on-par with xml2json, so is that not worth considering? – Jimmy Apr 16 '15 at 17:09
  • 1
    yes it is.. but you asked "which one do I choose". If you've already chosen xslt... get on with it! :) – gbjbaanb Apr 17 '15 at 07:24