sweet_xml
sweet_xml copied to clipboard
Is it possible to turn an xmlElement into text
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>
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