Skip to content

Instantly share code, notes, and snippets.

@sntran
Forked from sasa1977/xmerl_demo.ex
Last active August 29, 2015 14:10
Show Gist options
  • Save sntran/4e34d46830d160663b19 to your computer and use it in GitHub Desktop.
Save sntran/4e34d46830d160663b19 to your computer and use it in GitHub Desktop.

Revisions

  1. @sasa1977 sasa1977 revised this gist Sep 21, 2014. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions xmerl_demo.ex
    Original 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: to_unicode_binary(value)
    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: to_unicode_binary(value)
    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

    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(
  2. @sasa1977 sasa1977 revised this gist Sep 21, 2014. 1 changed file with 36 additions and 43 deletions.
    79 changes: 36 additions & 43 deletions xmerl_demo.ex
    Original file line number Diff line number Diff line change
    @@ -1,79 +1,72 @@
    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, []} =
    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
    |> to_unicode_char_list
    |> :binary.bin_to_list
    |> :xmerl_scan.string(options)
    from_element(doc)

    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)
    for child_element <- xpath(node, path) do
    child_element
    end
    end
    def first(node, path), do: node |> xpath(path) |> take_one |> from_element

    def first(node, path), do: node |> xpath(path) |> take_one
    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 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([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 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.element)
    :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)

    defp to_unicode_char_list(input) do
    input
    |> to_unicode_binary
    |> bitstring_to_list
    end
    end

    doc = XmlNode.from_string(%s(

    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")
  3. @sasa1977 sasa1977 revised this gist Dec 11, 2013. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions xmerl_demo.ex
    Original 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_binary(any)

    defp to_unicode_binary(any), do: to_string(any)
    defp to_unicode_char_list(input) do
    input
    |> to_unicode_binary
    |> to_char_list
    |> bitstring_to_list
    end
    end

    doc = XmlNode.from_string(%b(
    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")
  4. @sasa1977 sasa1977 revised this gist Jul 10, 2013. No changes.
  5. @sasa1977 sasa1977 created this gist Jul 10, 2013.
    82 changes: 82 additions & 0 deletions xmerl_demo.ex
    Original 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
    )