节点中的JSTOXML转换器模块未以正确的结构解析数据

jstoxml converter module in node not parsing data in correct structure

本文关键字:结构 数据 JSTOXML 转换器 模块 节点      更新时间:2023-09-26

我正在尝试使用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(和另一种类型ItemData类型用于根 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>