Xml library with control over how empty tags are printed


I’m looking for a (reliable, stable) library for printing data in an xml-format. What I additionally need is the ability to specify that empty tags should not be abbreviated (don’t ask, banks are apparently not able to read and understand specifications). For example, an empty tag some_tag should be printed like so:


and never like so (which is allowed according to the specification and also the preferred way):


I looked at xmerl and did not find this feature. Which other libraries do exist that allow this ?

I added this functionality to xml_builder_ex.

See empty: :full option in a call to generate/2.


Very cool ! The library looks neat. I’ll try it.

It works almost like expected. The only problem is that empty tags have indentation whitespaces as content. I think deleting lines 171 and 192 in file lib/xml_builder.ex would fix it.

Also, the readme defines the dependency as {:xml_builder, "~> 3.1"} but it only works if it is defined as {:xml_builder_ex, "~> 3.1"}.

1 Like

It’s not a problem, it is how it worked by design. There is an ability to provide a format: option to generate/2 and all the formatters would use it. Just gluing empty tags would violate this, so you always had an option to do format: [*: :indent, tag1: :none, tag2: :none].

But since it’s a common expectation to have empty tags glued, I added empty: :squeezed alongside empty: :full which does not respect the format setting and glues empty tags in (example from tests.)

1 Like

I’m not sure I understand. Doesn’t the indentation need to be preceded by a line break ? How does a parser know that b in this xml is empty and not equal to two blanks:

  <b>  </b>

Use empty: :squeezed if you need it to be <b></b>. I am to leave empty: :full for the cases when users want {:b, nil, nil} AST to be transformed into <b> </b>.