-
-
Save sntran/4e34d46830d160663b19 to your computer and use it in GitHub Desktop.
Revisions
-
sasa1977 revised this gist
Sep 21, 2014 . 1 changed file with 2 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -26,20 +26,17 @@ defmodule XmlNode do def node_name(node), do: elem(node, 1) def attr(node, name), do: node |> xpath('./@#{name}') |> extract_attr defp extract_attr([xmlAttribute(value: value)]), do: List.to_string(value) defp extract_attr(_), do: nil def text(node), do: node |> xpath('./text()') |> extract_text defp extract_text([xmlText(value: value)]), do: List.to_string(value) defp extract_text(_x), do: nil defp xpath(nil, _), do: [] defp xpath(node, path) do :xmerl_xpath.string(to_char_list(path), node) end end doc = XmlNode.from_string( -
sasa1977 revised this gist
Sep 21, 2014 . 1 changed file with 36 additions and 43 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,79 +1,72 @@ defmodule XmlNode do require Record Record.defrecord :xmlAttribute, Record.extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl") Record.defrecord :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl") def from_string(xml_string, options \\ [quiet: true]) do {doc, []} = xml_string |> :binary.bin_to_list |> :xmerl_scan.string(options) doc end def all(node, path) do for child_element <- xpath(node, path) do child_element end end def first(node, path), do: node |> xpath(path) |> take_one defp take_one([head | _]), do: head defp take_one(_), do: nil def node_name(nil), do: nil def node_name(node), do: elem(node, 1) def attr(node, name), do: node |> xpath('./@#{name}') |> extract_attr defp extract_attr([xmlAttribute(value: value)]), do: to_unicode_binary(value) defp extract_attr(_), do: nil def text(node), do: node |> xpath('./text()') |> extract_text defp extract_text([xmlText(value: value)]), do: to_unicode_binary(value) defp extract_text(_x), do: nil defp xpath(nil, _), do: [] defp xpath(node, path) do :xmerl_xpath.string(to_char_list(path), node) end defp to_unicode_binary(list) when is_list(list), do: :unicode.characters_to_binary(list) defp to_unicode_binary(any), do: to_string(any) end doc = XmlNode.from_string( """ <root> <child id="1">Saša</child> <child id="2">Jurić</child> </root> """ ) Enum.each(XmlNode.all(doc, "//child"), fn(node) -> IO.puts "#{XmlNode.node_name(node)} id=#{XmlNode.attr(node, "id")} text=#{XmlNode.text(node)}" end) IO.puts( doc |> XmlNode.first("//child[@id='2']") |> XmlNode.text ) IO.puts( doc |> XmlNode.first("//child[@id='3']") |> XmlNode.text ) IO.puts( doc |> XmlNode.first("//root") -
sasa1977 revised this gist
Dec 11, 2013 . 1 changed file with 18 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,37 +1,37 @@ defrecord :xmlAttribute, Record.extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl") defrecord :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl") defrecord XmlNode, element: nil do def from_string(xml_string, options // [quiet: true]) do {doc, []} = xml_string |> to_unicode_char_list |> :xmerl_scan.string(options) from_element(doc) end defp from_element(element), do: __MODULE__.new(element: element) defmacrop empty_node, do: __MODULE__[element: nil] def all(node, path) do lc child_element inlist xpath(node, path) do from_element(child_element) end end def first(node, path), do: node |> xpath(path) |> take_one |> from_element defp take_one([head | _]), do: head defp take_one(_), do: nil def node_name(empty_node()), do: nil def node_name(node), do: elem(node.element, 1) def attr(node, name), do: node |> xpath('./@#{name}') |> extract_attr defp extract_attr([:xmlAttribute[value: value]]), do: to_unicode_binary(value) defp extract_attr(_), do: nil def text(node), do: node |> xpath('./text()') |> extract_text defp extract_text([:xmlText[value: value]]), do: to_unicode_binary(value) defp extract_text(_), do: nil @@ -40,40 +40,40 @@ defrecord XmlNode, element: nil do defp xpath(node, path) do :xmerl_xpath.string(to_char_list(path), node.element) end defp to_unicode_binary(list) when is_list(list), do: :unicode.characters_to_binary(list) defp to_unicode_binary(any), do: to_string(any) defp to_unicode_char_list(input) do input |> to_unicode_binary |> bitstring_to_list end end doc = XmlNode.from_string(%s( <root> <child id="1">Saša</child> <child id="2">Jurić</child> </root> )) Enum.each(XmlNode.all(doc, "//child"), fn(node) -> IO.puts "#{XmlNode.node_name(node)} id=#{XmlNode.attr(node, "id")} text=#{XmlNode.text(node)}" end) IO.puts( doc |> XmlNode.first("//child[@id='2']") |> XmlNode.text ) IO.puts( doc |> XmlNode.first("//child[@id='3']") |> XmlNode.text ) IO.puts( doc |> XmlNode.first("//root") -
sasa1977 revised this gist
Jul 10, 2013 . No changes.There are no files selected for viewing
-
sasa1977 created this gist
Jul 10, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,82 @@ defrecord :xmlAttribute, Record.extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl") defrecord :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl") defrecord XmlNode, element: nil do def from_string(xml_string, options // [quiet: true]) do {doc, []} = xml_string |> to_unicode_char_list |> :xmerl_scan.string(options) from_element(doc) end defp from_element(element), do: __MODULE__.new(element: element) defmacrop empty_node, do: __MODULE__[element: nil] def all(node, path) do lc child_element inlist xpath(node, path) do from_element(child_element) end end def first(node, path), do: node |> xpath(path) |> take_one |> from_element defp take_one([head | _]), do: head defp take_one(_), do: nil def node_name(empty_node()), do: nil def node_name(node), do: elem(node.element, 1) def attr(node, name), do: node |> xpath('./@#{name}') |> extract_attr defp extract_attr([:xmlAttribute[value: value]]), do: to_unicode_binary(value) defp extract_attr(_), do: nil def text(node), do: node |> xpath('./text()') |> extract_text defp extract_text([:xmlText[value: value]]), do: to_unicode_binary(value) defp extract_text(_), do: nil defp xpath(empty_node(), _), do: [] defp xpath(node, path) do :xmerl_xpath.string(to_char_list(path), node.element) end defp to_unicode_binary(list) when is_list(list), do: :unicode.characters_to_binary(list) defp to_unicode_binary(any), do: to_binary(any) defp to_unicode_char_list(input) do input |> to_unicode_binary |> to_char_list end end doc = XmlNode.from_string(%b( <root> <child id="1">Saša</child> <child id="2">Jurić</child> </root> )) Enum.each(XmlNode.all(doc, "//child"), fn(node) -> IO.puts "#{XmlNode.node_name(node)} id=#{XmlNode.attr(node, "id")} text=#{XmlNode.text(node)}" end) IO.puts( doc |> XmlNode.first("//child[@id='2']") |> XmlNode.text ) IO.puts( doc |> XmlNode.first("//child[@id='3']") |> XmlNode.text ) IO.puts( doc |> XmlNode.first("//root") |> XmlNode.first("child[@id='1']") |> XmlNode.text )