sweet_xml icon indicating copy to clipboard operation
sweet_xml copied to clipboard

Is it possible to turn an xmlElement into text

Open jhonathas opened this issue 3 years ago • 1 comments

For example:

<features>
  <feature>example 1</feature>
  <feature>example 1</feature>
</features>

xpath(doc, ~x"./features") |> to_string()

And I would like it to print the exact block in XML format instead of an xmlElement

<features>
  <feature>example 1</feature>
  <feature>example 1</feature>
</features>

jhonathas avatar Dec 04 '22 19:12 jhonathas

You can do it like this:

xml = """
<features>
  <feature>example 1</feature>
  <feature>example 1</feature>
</features>
"""
extract = SweetXml.xpath(xml, ~x"/features")
['<?xml version=\"1.0\"?>' | xml_snippet] = :xmerl.export([extract], :xmerl_xml)
Enum.join(xml_snippet, "") |> IO.puts

There is also a PR to add something similar to the library

Adzz avatar Dec 05 '22 14:12 Adzz