节点中的JSTOXML转换器模块未以正确的结构解析数据
jstoxml converter module in node not parsing data in correct structure
我正在尝试使用jstoxml模块在我的Node js服务中将JSON对象转换为XML。我的输入结构是:
{
"user": "505723c5750c1fa2177682ed",
"uri": "http://localhost:3000/users/505723c5750c1fa2177682ed/items",
"items": [
{
"uri": "http://localhost:3000/items/1"
},
{
"uri": "http://localhost:3000/items/2"
}
],
"info": "blah."
}
我期望的结果是:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
我得到的结果是:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
如果有人遇到类似的问题,请帮助我。是否有任何其他 npm 节点包可以使其以预期的结构进行?
谢谢
免责声明:我是Jsonix的作者,Jsonix是一个用于XML<->JS转换的开源JavaScript库。Jsonix可用于Node.js(见这里(。
Jsonix 可以根据映射在 XML 和 JSON 之间进行转换(双向(。该映射为您提供了在 JSON 和 XML 之间进行转换时的灵活性。这可能是您在这里需要的。
我会让代码说话。下面是 JSON>XML 转换的演示案例:
- https://github.com/highsource/jsonix/tree/master/nodejs/demos/user
映射:
var Mapping = {
name : 'Mapping',
typeInfos : [ {
localName : 'Data',
propertyInfos : [ {
name : 'user'
}, {
name : 'uri'
}, {
name : 'items',
collection : true,
typeInfo : '.Item'
}, {
name : 'info'
} ]
}, {
localName : 'Item',
propertyInfos : [ {
name : 'uri'
} ]
} ],
elementInfos : [ {
elementName : {
localPart : 'data'
},
typeInfo : '.Data'
} ]
};
module.exports.Mapping = Mapping;
这里我们有两种类型:根类型(我称之为Data
(和另一种类型Item
。Data
类型用于根 XML 元素data
。
好的,现在编组代码:
// Create Jsonix context
var context = new Jsonix.Context([ Mapping ]);
var data = {
name : new Jsonix.XML.QName('data'),
value : {
"user" : "505723c5750c1fa2177682ed",
"uri" : "http://localhost:3000/users/505723c5750c1fa2177682ed/items",
"items" : [ {
"uri" : "http://localhost:3000/items/1"
}, {
"uri" : "http://localhost:3000/items/2"
} ],
"info" : "blah."
}
};
var marshaller = context.createMarshaller();
console.log(marshaller.marshalString(data));
下面是您获得的 XML:
<data>
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
</data>
链接:
- GitHub 上的 Jsonix 项目
- Jsonix 文档
如果可以将XML与JSON匹配,请检查其他库,例如xml2js。
我的问题得到了解决方案。在这里你可以得到它。这是一个在线演示。通过导出他们提供的代码,我们可以实现它,
感谢您使用我的图书馆! 对于延误,我深表歉意。 您可以使用以下结构实现该输出:
[
{"user": "505723c5750c1fa2177682ed"},
{"uri": "http://localhost:3000/users/505723c5750c1fa2177682ed/items"},
[
{"items": {"uri": "http://localhost:3000/items/1"}},
{"items": {"uri": "http://localhost:3000/items/1"}}
],
{"info": "blah."}
]
输出:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<info>blah.</info>
相关文章:
- 从键值结构中获取数据,并将其与AngularJS中ng重复的值进行比较
- 从json对象聚集数据并创建层次结构
- JS库支持各种数据结构?(如爪哇的番石榴)
- JavaScript数据结构
- Node JS,传统的数据结构?(如Set等),任何类似Java.util的node
- 更正扁平数据模型和noSQL数据结构
- 如何解析文本区域中的结构化字符串数据(接近JSON)以检索其所需的属性
- Firebase-结构数据
- 用于筛选无模式集合的最快数据结构
- 将数据结构转换为二进制数据
- JavaScript 设置具有对数搜索时间的数据结构
- 更好的数据结构来处理这个数组
- D3:使用 nest 函数将带有父键的平面数据转换为层次结构
- Firebase 数据结构理念
- 基于其他数据结构更新 AngularJS 中的数据结构
- JavaScript - JSON 数据结构的构建 - 如何使用变量值更改键名
- 如何为选中的项准备xml结构数据
- 用于报告层次结构数据的下拉列表
- 存储“最近10年”的数据结构数据集
- 如何从url中检索HTML结构数据,并使用javascript分析DOM特定数据