简体   繁体   中英

Parsing JSON object to XML

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.

如果要对生成的XML的元素和属性进行精细控制,一种选择是使用XSLT转换。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM