微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法将 XML 输入中的内容“复制和粘贴”到 DataWeave 中的 JSON 输出?

如何解决有没有办法将 XML 输入中的内容“复制和粘贴”到 DataWeave 中的 JSON 输出?

我有一个奇怪的案例需要帮助:我有一个 XML 文档,我希望使用 DW 将其转换为 JSON,这很容易,但我也希望获取一些属性并粘贴原始 XML 内容的某些部分作为他们的价值观,以避免在我的工作管道中出现接口问题。

更具体地说,我希望获取符合该类型元素列表的特定标签的所有元素,并在 XML 中以字符串形式返回相同的列表。示例场景如下所示:

输入

<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>

  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>

输出

{"book": "<book category=\"cooking\"><title lang=\"en\">EverydayItalian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book><book category=\"children\"><title lang=\"en\">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book>"}

这就是我达到接近解决方案的程度。在这种情况下,Report_DataReport_Entry 只是包装信息。我希望收到各种 Report_Entry 元素,所以这仍然是一个草稿。

%dw 2.0
import * from dw::core::Types
import mergeWith from dw::core::Objects
input payload xml
output application/json

var literalKeys = (namesOf(payload.Report_Data.Report_Entry) distinctBy ((item,index) -> item)) filter ((item) -> not isObjectType(typeOf(payload.Report_Data.Report_Entry[item])))

var objectKeys = (namesOf(payload.Report_Data.Report_Entry) distinctBy ((item,index) -> item)) filter ((item) -> isObjectType(typeOf(payload.Report_Data.Report_Entry[item])))

var allObjects = payload.Report_Data.Report_Entry filterObject((value,key,index) -> objectKeys reduce ((item,accumulator = false) -> (key ~= item) or accumulator))

var justTheScalars = payload.Report_Data.Report_Entry filterObject((value,index) -> not (objectKeys reduce ((item,accumulator = false) -> (key ~= item) or accumulator)))

var groupedAllObjects = allObjects groupBy (item,index) -> index

---

justTheScalars mergeWith (groupedAllObjects)

我已经到了我能做到的地步

  1. 确定哪些键对应于对象,哪些对应于文字
  2. 从我的输入中划分所有对象和所有标量
  3. 根据类将对象分组并返回合并对象

但我仍然想念我可以以某种方式将输入和粘贴作为字符串或以某种方式将其转换为字符串以避免尽可能多的信息丢失的部分。

请考虑到这种情况是 DW 使用的特殊情况,因为我没有关于不同 Report_Entry 中会出现什么的信息,这就是为什么我正在使用函数组合来动态获取键和值。

解决方法

让 DataWeave 知道您想将每本书编写为 XML 字符串更简单。

%dw 2.0
output application/json
---
payload.bookstore  mapObject {
    book: write({($$): $},"application/xml",{writeDeclaration:false})
}

输出:

{
  "book": "<book category=\"cooking\">\n  <title lang=\"en\">Everyday Italian</title>\n  <author>Giada De Laurentiis</author>\n  <year>2005</year>\n  <price>30.00</price>\n</book>","book": "<book category=\"children\">\n  <title lang=\"en\">Harry Potter</title>\n  <author>J K. Rowling</author>\n  <year>2005</year>\n  <price>29.99</price>\n</book>"
}

我更新了解决方案以发出属性并避免 XML 声明。

,

抱歉 - 我有点困惑。您只是想将输入序列化为字符串?

输入:

<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>

  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>

数据编织:

%dw 2.0
output application/json
---
payload.bookstore.*book map {
    "book": write({ book: $ },'application/xml',{writeDeclaration: false,indent: false})
}

输出:

[
  {
    "book": "<book><title lang=\"en\">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book>"
  },{
    "book": "<book><title lang=\"en\">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book>"
  }
]

编辑:

如果你只想要一个字符串数组:

%dw 2.0
output application/json
---
payload.bookstore.*book map write({ book: $ },indent: false})

输出:

[
  "<book><title lang=\"en\">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book>","<book><title lang=\"en\">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book>"
]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。