【问题标题】:How to get access to xml2js result properties如何访问 xml2js 结果属性
【发布时间】:2018-04-07 08:44:15
【问题描述】:

您好,我已经使用节点 js 中的 xml2js 包将 Xml 解析为 JSON。结果如下..

现在我需要获取 JSON 对象的属性,比如

result.env:Envelope

但它会产生编译时错误。我如何访问该对象。有什么帮助吗??

【问题讨论】:

    标签: json node.js xml xml2js


    【解决方案1】:

    您可以通过result['env:Envelope']访问它。

    假设你有如下命名空间的xml:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
       <s:Body>
          <GetItems xmlns="http://www.test.com">
             <Items>
                <Object d3p1:type="Fruits" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance">
                   <Key>11032896</Key>
                   <Name>Apple</Name>
                </Object>
                <Object d3p1:type="Fruits" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance">
                   <Key>11662896</Key>
                   <Name>Banana</Name>
                </Object>
            </Items>
        </GetItems>
        </s:Body>
    </s:Envelope>
    

    您可以访问它而不会在 TypeScript 中出现编译错误

    result['Envelope']['Body']['GetItems']['Items']['Object']
    

    【讨论】:

    • 虽然这可以回答这个问题,但它非常非常吝啬。您应该添加一两句话来解释它是如何工作的,也许还应该添加一个指向适当文档的链接。
    猜你喜欢
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    • 2019-01-25
    相关资源
    最近更新 更多