XML-JSON 转换器规则
我正在使用 net.sf.json.xml.XMLSerializer 将 XML 文档转换为 JSON。对于几乎两个相似的 XML 文档,我得到了两个不同的结果。我的代码是:
public static void main(String[] args) throws DocumentException {
String t1="<A><B>aa</B><C><D>Martin Ritt</D> </C></A>";
String t2="<A><B>aa</B><C><D>Martin Ritt</D></C></A>";
System.out.println(new XMLSerializer().read(t1).toString());
System.out.println(new XMLSerializer().read(t2).toString());
}
第一个字符串 t1 转换为:
{"B":"aa","C":["Martin Ritt"]}
而 t2 转换为:
{"B":"aa","C":{"D":"Martin Ritt"}}
这意味着,在第一种情况下,C 被视为数组,而在第二种情况下,它被视为对象。两个 XML 之间的区别在于 D 元素结束后的空格。即在 之后。
知道这是怎么回事吗?规则是什么?我更感兴趣的是如何让它一致地识别数组。
I'm using net.sf.json.xml.XMLSerializer to convert XML documents to JSON. I'm getting two different results for almost two similar XML documents. My code is:
public static void main(String[] args) throws DocumentException {
String t1="<A><B>aa</B><C><D>Martin Ritt</D> </C></A>";
String t2="<A><B>aa</B><C><D>Martin Ritt</D></C></A>";
System.out.println(new XMLSerializer().read(t1).toString());
System.out.println(new XMLSerializer().read(t2).toString());
}
The first string t1 is converted to:
{"B":"aa","C":["Martin Ritt"]}
while t2 is converted to:
{"B":"aa","C":{"D":"Martin Ritt"}}
That means, in the first case, C is considered an array while in the second case, it is considered an object. The difference between the two XMLs is the space after the closing of the D element. That is, after </D>
.
Any idea what is going on here? what is the rule? I'm more interested in how to make it recognize arrays consistently.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
规则是:
具有文本节点子节点的元素节点 =>
具有子元素的元素节点=>
具有混合子节点的元素节点(元素+空白)=>
参考
XSLTJSON:使用 XSLT 将 XML 转换为 JSON
使用 XSLT 将 XML 转换为 JSON
映射混合内容
识别节点类型
The rules are:
<A>
Element nodes with text node children =>
Element nodes with element children =>
Element nodes with mixed children (element + white space) =>
References
XSLTJSON: Transforming XML to JSON using XSLT
Convert XML to JSON using XSLT
Mapping Mixed Content
Identify Node Type