ElixirXML

ElixirXML (based in Elixml) is an improved and optimized XML library for Elixir that makes it easy to:

Installation

If available in Hex, the package can be installed by adding elixir_xml to your list of dependencies in mix.exs:

def deps do
  [
    {:elixir_xml, github: "https://github.com/OscarTinajero117/elixir_xml"}
  ]
end

Usage

Parse an XML file

Consider the following example XML file my_file.xml:

<root>
  <group_a id="1">
    <el1>a_1</el1>
    <el2>a_2</el2>
  </group_a>
  <group_b>
    <el1>b_1</el1>
    <el2>b_2</el2>
  </group_b>
  <group_c>
    <child1>
      <subchild>foo</subchild>
    </child1>
  </group_c>
</root>

You can load it with:

mydoc =
  File.read!("my_file.xml")
  |> ElixirXML.parse

# returns the root element like
# %{name: "root", children: [...], attributes: [...]}

Find elements

ElixirXML.find(mydoc, "//group_a")
# returns the list of elements found (only one)

Access text

ElixirXML.find(mydoc, "//subchild") |> hd |> ElixirXML.text()
# returns "foo"

Access attributes

ElixirXML.find(mydoc, "//group_a") |> hd |> ElixirXML.attribute("id")
# returns "1"

Reconstruct XML Document

child1 = ElixirXML.find(mydoc, "//child1") |> hd
ElixirXML.format_document(child1)
# returns
# """
#  <?xml version="1.0" encoding="UTF-8"?>
#  <child1>
#    <subchild>foo</subchild>
#  </child1>
# """

Features

Documentation

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ElixirXML.