1

I'm running into an issue using Groovy to add new child nodes to an XML Document. I don't want any children in the new node, it's just a placeholder at this point. Basically, the code looks like

trans = new Node(parent, translationsName, new HashMap(), new HashMap(), "")
parent.children.add(trans)

However, when I check the XML, all that is added is

<NODE/>

When what I want is something like...

<NODE>
</NODE>

I've been scouring the internet and reading the documentation, but to no avail. Does anyone have any experience with this? I can't seem to figure out what I'm doing wrong. Thanks!

AndrewSmiley
  • 1,933
  • 20
  • 32

2 Answers2

1

You are just looking at the self-closing syntax for an empty node. Note the slash at the end, not at the beginning as it would be for a closing tag. You just have two ways to render the same empty node.

Depending on how you are rending you can sometimes ask for either form, e.g. Groovy's XmlNodePrinter has an "expandEmptyElements" property. Well-behaving XML tools should support either variation.

Paul King
  • 627
  • 4
  • 6
1

If you really need also the line break there, then you have to add a text node (with the line break).

cfrick
  • 35,203
  • 6
  • 56
  • 68