I know there should be an “easy” way to do this, but I cannot seem to find it.
I want to get the name of the root element of an XML document, I am using SweetXml
.
1 Like
I would go for SweetXml.stream!(doc, options_callback)
, the example in HexDocs should give you a good starting point
Not necessarily the best solution, but you can try the following:
import SweetXml
doc = "<body><header><p>Message</p><ul><li>One</li><li><a>Two</a></li></ul></header></body>"
doc |> SweetXml.parse() |> elem(1)
which gives as outcome:
:body
iex> import SweetXml
iex> "<hello>world</hello>" |> SweetXml.parse() |> xpath(~x"name(/*)"s)
"hello"
Stolen from this GitHub issue response.
Side note: That issue thread, although short, taught me a lot of useful information.
2 Likes
Thanks!
And it can be even shorter, as you don’t need to parse the document first.
xpath(doc, ~x"name(/*)"s)
3 Likes