将 XML 解析为 JSON Elixir
Posted
技术标签:
【中文标题】将 XML 解析为 JSON Elixir【英文标题】:Parse XML to JSON Elixir 【发布时间】:2022-01-04 20:27:36 【问题描述】:任何人都知道如何将 xml 转换为 json。我试过 Sweetyxml 但它没有将 xml 转换为 json。
使用这个要点 https://gist.github.com/spint/40717d4e6912d8cea929 读取json
:ok, xmldoc = File.read(Path.expand("/Users/sohaibanwar/Desktop/unconfirmed_order.xml"))
doc, [] = xmldoc |> to_charlist() |> :xmerl_scan.string()
After parsing (in screenshot), but not getting right answer
我正在使用的 YML 文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order>
<orderId>35267684</orderId>
<orderToken>fa74171d-f54a-4e76-bcf2-2dd284ac6450</orderToken>
<brokerTicketId>169855177</brokerTicketId>
<section>SEC1</section>
<row>N</row>
<seats/>
<notes>Limited view. Please note that you will need to use an ios or android mobile device to gain entry to your event.</notes>
<quantity>2</quantity>
<cost>324.00</cost>
<event>Dave Matthews Band (Rescheduled from 7/17/2020, 7/16/2021)</event>
<eventDate>2021-08-20 19:30:00</eventDate>
<orderDate>2021-06-18 05:43:13</orderDate>
<expectedShipDate>2021-06-18 00:00:00</expectedShipDate>
<venue>Xfinity Center - MA - Mansfield, MA</venue>
<status>UNCONFIRMED</status>
<purchaseOrderId>35088971</purchaseOrderId>
<electronicDelivery>false</electronicDelivery>
<passThrough></passThrough>
<listingId>3359267717</listingId>
<productionId>3412459</productionId>
<eventId>218</eventId>
<zone>false</zone>
<barCodesRequired>false</barCodesRequired>
<transferViaURL>true</transferViaURL>
<instantTransfer>false</instantTransfer>
<instantFlashSeats>false</instantFlashSeats>
</order>
要求的结果 您可以在此处粘贴 XML 以获得所需的答案
"order":
"orderId": 35267684,
"orderToken": "fa74171d-f54a-4e76-bcf2-2dd284ac6450",
"brokerTicketId": 169855177,
"section": "SEC1",
"row": "N",
"seats": "",
"notes": "Limited view. Please note that you will need to use an iOS or Android mobile device to gain entry to your event.",
"quantity": 2,
"cost": 324,
"event": "Dave Matthews Band (Rescheduled from 7/17/2020, 7/16/2021)",
"eventDate": "2021-08-20 19:30:00",
"orderDate": "2021-06-18 05:43:13",
"expectedShipDate": "2021-06-18 00:00:00",
"venue": "Xfinity Center - MA - Mansfield, MA",
"status": "UNCONFIRMED",
"purchaseOrderId": 35088971,
"electronicDelivery": false,
"passThrough": "",
"listingId": 3359267717,
"productionId": 3412459,
"eventId": 218,
"zone": false,
"barCodesRequired": false,
"transferViaURL": true,
"instantTransfer": false,
"instantFlashSeats": false
【问题讨论】:
绝对不清楚您要实现什么目标。请提出MCVE。 对不起@AlekseiMatiushkin 我说得更清楚了 【参考方案1】:好吧,如果您的问题是 How to convert xml to json in Elixir
,答案很简单:使用 https://github.com/homanchou/elixir-xml-to-map 和您选择的 JSON 编码器:
def xml2json(path) do
File.read!(path)
|> XmlToMap.naive_map()
|> Jason.encode!()
end
这个答案的问题是 XML 不是同构映射(您在 XML 中有标签和属性,而在映射或 JSON 中没有),所以更好的方法是使用SweetXML
(或@ 987654327@) 并使用适当的匹配执行 xmap ,如此处(代码来自 SweetXML 示例 - https://github.com/kbrw/sweet_xml#examples):
result = doc |> xmap(
matchups: [
~x"//matchups/matchup"l,
name: ~x"./name/text()",
winner: [
~x".//team/id[.=ancestor::matchup/@winner-id]/..",
name: ~x"./name/text()"
]
],
last_matchup: [
~x"//matchups/matchup[last()]",
name: ~x"./name/text()",
winner: [
~x".//team/id[.=ancestor::matchup/@winner-id]/..",
name: ~x"./name/text()"
]
]
)
assert result == %
matchups: [
%name: 'Match One', winner: %name: 'Team One',
%name: 'Match Two', winner: %name: 'Team Two',
%name: 'Match Three', winner: %name: 'Team One'
],
last_matchup: %name: 'Match Three', winner: %name: 'Team One'
另一种选择是使用https://github.com/willemdj/erlsom 并手动遍历它从simple_form
调用发出的元组树。请注意,无论如何您都必须处理 XMLNS 和 attr/value 问题。
【讨论】:
以上是关于将 XML 解析为 JSON Elixir的主要内容,如果未能解决你的问题,请参考以下文章
PHP - xml到json转换:“无法将字符串解析为XML”
在 Azure 数据工厂中使用复制数据活动将 xml 解析为 json 时如何删除转义字符?