1

I am trying to figure out the best approach for custom parsing of JSON object into an XML document.

Given the following JSON (I use JSON.org):

{"CfgAccessGroup":{"CfgGroup":{"capacityRuleDBID":{"value":0},"DBID":{"value":97},"siteDBID":{"value":0},"name":{"value":"EVERYONE"},"quotaTableDBID":{"value":0},"contractDBID":{"value":0},"state":{"value":1},"capacityTableDBID":{"value":0},"tenantDBID":{"value":1}},"xmlns":"http://schemas.genesyslab.com/Protocols/Configuration/ConfServer/2005/","type":{"value":6},"memberIDs":{"CfgID":[{"CSID":{"value":0},"DBID":{"value":5195},"type":{"value":3}},{"CSID":{"value":0},"DBID":{"value":12854},"type":{"value":3}},{"CSID":{"value":0},"DBID":{"value":12863},"type":{"value":3}},{"CSID":{"value":0},"DBID":{"value":5808},"type":{"value":3}}]}}}

I have tried to basically reconvert the JSON to XML by doing:

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = db.parse(new InputSource(new StringReader(XML.toString(object))));

However, the output is:

<CfgAccessGroup>
    <CfgGroup>
        <capacityRuleDBID>
            <value>
            0
            </value>
        </capacityRuleDBID>
        ...
    <CfgGroup>
</CfgAccessGroup>

I need it to be:

...
<capacityRuleDBID value=0 />
...

Honestly I don't really know where to start.

aur8l
  • 125
  • 13
  • maybe this helps https://stackoverflow.com/questions/4056419/json-corresponding-to-an-xml-with-attributes – pL4Gu33 Oct 30 '18 at 23:49

1 Answers1

0

If you want fine grained control over elements and attributes of generated XML, one option is to use XSLT transformation.

Prasanth Nair
  • 489
  • 3
  • 12
  • 1
    Concur. No off-the-shelf JSON-to-XML conversion library is always going to give you the exact XML that you want. Some give more control than others, but in the end, if you know exactly what you want, then you're going to have to do some coding; and the most practical way is to take the output of a library method and then apply XSLT to transform it. (Therefore, one approach is to use the XSLT 3 json-to-xml() method as your first step, followed by custom transformation). – Michael Kay Oct 31 '18 at 08:49