Created
September 29, 2015 19:08
-
-
Save spint/40717d4e6912d8cea929 to your computer and use it in GitHub Desktop.
Elixir XML processing (to JSON) with SweetXML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mix.exs file dependencies: | |
# defp deps do | |
# [ | |
# {:sweet_xml, "~> 0.4.0"}, | |
# {:json, "~> 0.3.0"} | |
# ] | |
# end | |
defmodule ElixirXml do | |
import SweetXml | |
def count_items(file) do | |
File.stream!(file, [:read]) |> xpath(~x"//PatBase/Family/Patent"l) |> length | |
end | |
def elements(file) do | |
# s-option forces to return strings instead of char lists | |
File.stream!(file, [:read]) |> xpath(~x"//PatBase/Family/Patent"l, title: ~x"./Title/btitle/text()"s, pub_date: ~x"./PublicationDate/text()"s, pub_number: ~x"./PublicationNumber/text()"s ) | |
end | |
def parse(file) do | |
elements(file) |> to_json | |
end | |
defp to_json(attr_array) do | |
JSON.encode(attr_array) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
anyone knows How to parse xml into json I tried Sweetyxml but its not converting xml into json.
Using this gist
https://gist.github.com/spint/40717d4e6912d8cea929
Reading 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 FILE
Required Results
You can paster the XML here to get the required answer
https://jsonformatter.org/xml-parser