简体   繁体   中英

Write JSON for corresponding XML

I want to write JSON code which can be converted in fix format kind of XML

    <function>foo
      <return>uint32_t</return>
      <param>count
        <type>uint32_t</type>
      </param>
    </function>

I have tried multiple ways to develop a JSON which can be formatted like as in above but failed to get perfection because no separate key is required for foo and count which are orphan values otherwise.

Tried ways: Way 1:

{
"function" : 
  {"foo":
    {"return":"uint32_t"},
    "param":
      {"count":
        {"type":"uint32_t"}
      }
     }
   }

Way 2:

{
"function" : 
  ["foo",{"return":"uint32_t"}],
  "param":
    ["count",{"type":"uint32_t"}]
}

Way 3: But i do not need name tag :(

{
"function": 
  {"name": "foo",
   "return": "uint32_t",
   "param": "count",
   "type": "uint32_t"
  }
}

For generating output and testing please use: JSON to XML convertor

Requesting your help.. I later have a script to convert the formatted excel to C header files.

It is very rare for a JSON-to-XML conversion library to give you precise control over the XML that is generated, or conversely, for an XML-to-JSON converter to give you precise control over the JSON that is generated. It's basically not possible because the data models are very different.

Typically you have to accept what the JSON-to-XML converter gives you, and then use XSLT to transform it into the flavour of XML that you actually want.

(Consider using the json-to-xml() conversion function in XSLT 3.0 and then applying template rules to the result.)

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