将 XML 解析为 JSON Elixir
Parse XML to JSON Elixir
任何人都知道如何将 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
}
}
好吧,如果您的问题是 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
(或 :xmerl
)并像这里一样使用适当的匹配来做 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。我试过 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
}
}
好吧,如果您的问题是 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
(或 :xmerl
)并像这里一样使用适当的匹配来做 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 问题。