Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add doc_to_xml method for converting doc to xml string #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/sweet_xml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ defmodule SweetXml do
get_current_entities(parent, spec) |> spec.transform_fun.()
end


def xpath(parent, sweet_xpath, subspec) do
if sweet_xpath.is_list do
current_entities = xpath(parent, sweet_xpath)
Expand Down Expand Up @@ -554,6 +553,26 @@ defmodule SweetXml do
%{sweet_xpath | transform_fun: fun}
end

@doc ~S"""
Convert `doc` to XML string

## Examples

iex> import SweetXml
iex> "<h1><a>Some linked title</a></h1>"
...> |> parse()
...> |> doc_to_xml()
"<?xml version=\"1.0\"?><h1><a>Some linked title</a></h1>"
iex> "<body><header><ul><li>One</li><li><a>Two</a></li></ul></header></body>"
...> |> parse()
...> |> xpath(~x"/body/header/ul"e)
...> |> doc_to_xml()
"<?xml version=\"1.0\"?><ul><li>One</li><li><a>Two</a></li></ul>"
"""
def doc_to_xml(doc) do
:xmerl.export([doc], :xmerl_xml) |> List.flatten() |> List.to_string()
end

defp _value(entity) do
cond do
is_record? entity, :xmlText ->
Expand Down