首页 > 其他分享 >Converting between XML and JSON

Converting between XML and JSON

时间:2023-07-24 15:32:01浏览次数:39  
标签:XML text structured element content JSON Converting


1 Convertion between XML and JSON

http://www.json.org/java/index.html

 

 

API:

http://www.json.org/javadoc/org/json/XML.html

public class XML

extends java.lang.Object

 

This provides static methods to convert an XML text into a JSONObject, and to covert a JSONObject into an XML text.


Source:

http://www.json.org/java/org/json/XML.java



2 http://www.json.org/json-zh.html

有很多使用java处理json的介绍和project链接。

其中http://www.json.org/java/index.html是其中project的基础。

 

3 Converting Between XML and JSON

http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html


Converting Between XML and JSON
by Stefan Goessner
May 31, 2006

 
More and more web service providers seem to be interested in offering JSON APIs beneath their XML APIs. One considerable advantage of using a JSON API is its ability to provide cross-domain requests while bypassing the restrictive same domain policy of the XmlHttpRequest object. On the client-side, JSON comes with a native language-compliant data structure, with which it performs much better than corresponding DOM calls required for XML processing. Finally, transforming JSON structures to presentational data can be easily achieved with tools such as JSONT.

So if you're working in this space, you probably need to convert an existing XML document to a JSON structure while preserving the following:
structure
order
information

In an ideal world, the resulting JSON structure can be converted back to its original XML document easily. Thus it seems worthwhile to discuss some common patterns as the foundation of a potentially bidirectional conversion process between XML and JSON. A similar discussion can be found at BadgerFish and Yahoo-- without the reversibility aspect though.
A Pragmatic Approach

A single structured XML element might come in seven flavors:
an empty element
an element with pure text content
an empty element with attributes
an element with pure text content and attributes
an element containing elements with different names
an element containing elements with identical names
an element containing elements and contiguous text

The following table shows the corresponding conversion patterns between XML and JSON.Pattern  XML  JSON  Access

1 <e/>  "e": null  o.e 
2 <e>text</e>  "e": "text"  o.e 
3 <e name="value" />  "e":{"@name": "value"}  o.e["@name"] 
4 <e name="value">text</e>  "e": { "@name": "value", "#text": "text" }  o.e["@name"] o.e["#text"] 
5 <e> <a>text</a> <b>text</b> </e>  "e": { "a": "text", "b": "text" }  o.e.a o.e.b 
6 <e> <a>text</a> <a>text</a> </e>  "e": { "a": ["text", "text"] }  o.e.a[0] o.e.a[1] 
7 <e> text <a>text</a> </e>  "e": { "#text": "text", "a": "text" }  o.e["#text"] o.e.a

Please note that all patterns are considered to describe structured elements, despite the fact that the element of pattern 7 is commonly understood as a semistructured element. A pragmatic approach to convert an XML document to a JSON structure and vice versa can be based on the seven patterns above. It always assumes a normalized XML document for input and doesn't take into consideration the following:
XML declaration
processing instructions
explicit handling of namespace declarations
XML comments

 

Preserving order

JSON is built on two internal structures:
A collection of name/value pairs with unique names (associative array)
An ordered list of values (array)

An attempt to map a structured XML element...

<e>
  <a>some</a>
  <b>textual</b>
  <a>content</a>
</e>...to the following JSON object:
"e": {
  "a": "some",
  "b": "textual",
  "a": "content"
}

yields an invalid result, since the name "a" is not unique in the associative array. So we need to collect all elements of identical names in an array. Using the patterns 5 and 6 above yields the following result:

"e": {
  "a": [ "some", "content" ],
  "b": "textual"
}

Now we have a structure that doesn't preserve element order. This may or may not be acceptable, depending on whether the above XML element order matters.

So, our general rules of thumb are:

A structured XML element can be converted to a reversible JSON structure, if
all subelement names occur exactly once, or …
subelements with identical names are in sequence.

and

A structured XML element can be converted to an irreversible but semantically equivalent JSON structure, if
multiple homonymous subelements occur nonsequentially, and …
element order doesn't matter.

If none of these two conditions apply, there is no pragmatic way to convert XML to JSON using the patterns above. Here, SVG and SMIL documents, which implicitly rely on element order, come to mind.
Semi-Structured XML

XML documents can contain semi-structured elements, which are elements with mixed content of text and child elements, usually seen in documentation markup. If the textual content is contiguous, as in:

<e>
  some textual
  <a>content</a>
</e>

we can apply pattern 7 and yield the following for this special case:

"e": {
  "#text": "some textual",
  "a":  "content",
}

But how do we convert textual content mixed up with elements? For example:

<e>
  some
  <a>textual</a>
  content
</e>

It obviously doesn't make sense in most cases to collect all text nodes in an array,

"e": {
  "#text": ["some", "content"],
  "a": "textual"
}

that doesn't preserve order or semantics.

So the best pragmatic solution is to treat mixed semi-structured content in JSON the same way as XML treats CDATA sections -- as unknown markup.
"e": "some <a>textual</a> content"

Another rule is that XML elements with
mixed content of text and element nodes and
CDATA sections

are converted to a reversible JSON string containing the complete XML markup according to pattern 2 or 4.

 

Examples

Now let's look at two examples using the insight we've gained thus far. Microformats are well suited because they are an open standard and short enough for a brief discussion.

XOXO, as a simple XHTML-based outline format, is one of several microformats. The slightly modified sample from the Draft Specification reads:

<ol class="xoxo">
  <li>Subject 1
    <ol>
        <li>subpoint a</li>
        <li>subpoint b</li>
    </ol>
  </li>
  <li><span>Subject 2</span>
    <ol compact="compact">
        <li>subpoint c</li>
        <li>subpoint d</li>
    </ol>
  </li>
</ol>

Now we apply the patterns above to convert this XML document fragment to a JSON structure.
The outer list with two list items is converted using pattern 6.
The first list item contains a single textual content "Subject 1" and an inner list element. So, it can be treated according to pattern 7.
The first inner list is converted with pattern 6 again.
Pattern 5 is applied to the second item of the outer list.
The second inner list is converted using a combination of patterns 3 and 6.

Here is the resulting JSON structure, which is reversible without losing any information.

"ol": {
  "li": [ 
    {
      "#text": "Subject 1",
      "ol": {
        "li": ["subpoint a", "subpoint b"]
      }
    },
    {
      "span": "Subject 2",
      "ol": {
        "@compact": "compact",
        "li": ["subpoint c", "subpoint d"]
      }
    }
  ]
}

hCalendar is another microformat based on the iCalendar standard. We'll just ignore the fact that the iCalendar format could be more easily converted to JSON, and will look at an hCalendar event example, which is also slightly modified so that it is a structured, rather than mixed, semi-structured

document fragment.
<span class="vevent">
  <a class="url" href="http://www.web2con.com/">
    <span class="summary">Web 2.0 Conference</span>
    <abbr class="dtstart" title="2005-10-05">October 5</abbr>
    <abbr class="dtend" title="2005-10-08">7</abbr>
    <span class="location">Argent Hotel, San Francisco, CA</span>
  </a>
</span>

Here, patterns 2, 3, 4, 5 and 6 are used to generate the following JSON structure:

"span": {
  "a": {
    "@class": "url",
    "@href": "http://www.web2con.com/",
    "span": [
      { "@class": "summery", "#text": "Web 2.0 Conference" },
      { "@class": "location", "#text": "Argent Hotel, San Francisco, CA" }
    },
    "abbr": [
      { "@class": "dtstart", "title": "2005-10-05", "#text": "October 5" },
      { "@class": "dtend", "title": "2005-10-08", "#text": "7" }
    }
  }
}

This example demonstrates a conversion that does not preserve the original element order. Even if this may not change semantics here, we can do the following:
state that a conversion isn't sufficiently possible.
tolerate the result if order doesn't matter.
try to make our XML document more JSON-friendly.

In many cases the last point may be not acceptable, at least when the XML document is based on existing standards. But in other cases, it may be worth the effort to consider some subtle XML changes, which can make XML and JSON play nicely together. Changing the <abbr> elements to <span> elements in the hCalendar example would be an improvement.

XML is a document-centric format, while JSON is a format for structured data. This fundamental difference may be irrelevant, as XML is also capable of describing structured data. If XML is used to describe highly structured documents, these may play very well together with JSON.

Problems may arise, if XML documents do the following:
implicitly rely on element order
contain a lot of semi-structured data

As proof of this concept, I have implemented two Javascript functions -- xml2json and json2xml -- based on the six patterns above, which can be used for the following:
client-side conversion
a parsed XML document via DOM to a JSON structure
a JSON structure to a (textual) XML document
implementing converters in other server side languages

Future XML document design may be influenced by these or similar patterns in order to get the best of both the XML and JSON worlds.

标签:XML,text,structured,element,content,JSON,Converting
From: https://blog.51cto.com/u_11326739/6835599

相关文章

  • 【Azure Event Hub】Event Hub的Process Data页面无法通过JSON格式预览数据
    问题描述在EventHub的门户页面中,可以通过ProcessData页面查看EventHub中的数据,但是当使用JSON格式预览时(ViewinJSON),却出现错误。消息一: Nodatawasfoundforpreviewfrom'test01'.Makesuretheinputhasrecentlyreceiveddataandthecorrectformatofthosee......
  • .net core controller获取post的json数据
    如何在.NETCoreController中获取POST的JSON数据在开发Web应用程序时,我们经常需要从客户端发送一些数据到服务器端。一种常见的方式是使用HTTP的POST方法来发送数据。在.NETCore中,我们可以使用Controller来处理这些请求,并从POST请求中获取JSON数据。创建一个.NETCoreWebAPI......
  • 在线java 实体转 json
    在线Java实体转JSON的步骤和代码示例1.简介在Java开发中,我们经常需要将Java对象转换为JSON格式,以便在网络传输或者存储过程中使用。在本文中,我将向你介绍如何在线进行Java实体转JSON的处理,并提供相应的代码示例。2.实体转JSON的步骤下面是实现在线Java实体转JSON的整个流程,通......
  • vue3.0 外部配置文件一 (导入json文件方式)
    vue3.0外部配置文件,重点是打包后也可以修改配置参数 注:js文件中必须是标准的json格式一、在public中创建static文件夹,在static文件夹中创建config.json  文件 config.json (必须是标准的json格式){"webSocketUrl":"ws://192.168.1.120:5011/chat/","......
  • JSON化------日期类型转JSON后变为时间戳
    我们在进行对象JSON化的时候(其中包含一些日期类型字段)日期类型在进行JSON的时候会转换为时间戳解决JSON.parse(JSONObject.toJSONStringWithDateFormat(要json的值,"yyyy-MM-ddHH:mm:ss")).toString(); ......
  • android packages.xml 与pms启动
    实现"androidpackages.xml与pms启动"的步骤为了实现"androidpackages.xml与pms启动",我们需要按照以下步骤进行操作:步骤操作步骤1获取PackageManagerService(PMS)实例步骤2加载packages.xml文件步骤3初始化PackageManagerService(PMS)实例步骤4启动P......
  • 转!XML文件和DOM Document、String字符串三种类型之间的相互转换
    转自:https://www.cnblogs.com/fightingdeng1998/p/16047075.html用的包是org.w3c.dom.Document下的/***@Authordengzp*@Date2022/3/22*@Version1.0*@Description*/publicclassOperateXMLByDOM{/***将给定文件的内容或者给定URI的内容解析......
  • json-server
    前端必备技能:json-server全攻略在项目的初始阶段,后端提供的接口或数据可能是不完整的,作为一名前端开发工程师,不可避免的要使用mock的数据。如果此时的你不想使用简单的静态数据,而是想自己在本地启动一个server来模拟请求相关的操作,那么json-server是一个不错的选择。json-serve......
  • python将xml文件转换为json
    转载自:如何使用Python实现XML转JSON-知乎(zhihu.com)代码:importosimportjsonimportxmltodictdefxml_to_JSON(xml):#格式转换try:convertJson=xmltodict.parse(xml,encoding='utf-8')jsonStr=json.dumps(convertJson,indent=1)......
  • C#解析匿名JSON数据
    C#解析匿名JSON数据使用工具:Newtonsoft.Json使用方式://模拟数据varjsonData=JsonConvert.SerializeObject(new{Name="张三",Id=1,Age=18,UserName="123123"});JObjectjObject=JObject.Parse(jsonData);//解析数据varname=jObject["Name"......