I am using joox for XML manipulation.
One of my use cases is the removal of a tag/node under certain circumstances.
Example:
XML input, test.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<Parent>
<Data>
<Tag1>Apples</Tag1>
<Tag2>Banana</Tag2>
</Data>
</Parent>
Java code to load and manipulate XML:
final Match template = $(IOUtils.toString(Objects.requireNonNull(
MyClass.class.getClassLoader().getResourceAsStream("templates/test.xml")), StandardCharsets.UTF_8));
if (someCriteria()) {
template.find("Tag1").remove();
}
The output of the XML should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<Parent>
<Data>
<Tag2>Banana</Tag2>
</Data>
</Parent>
But instead it looks like this
<?xml version="1.0" encoding="UTF-8"?>
<Parent>
<Data>
<Tag2>Banana</Tag2>
</Data>
</Parent>
Stack Overflow doesn't pick it up, but the indentation whitespaces are still present, so I tried this additional line of code:
template.xpath("//text()[normalize-space(.) = '']").remove();
but with no success.
How do i remove an entire line with joox so that there is no gap in the xml output?
I think you need to try regx.
// first convert to string
String output = template.toString();
output = output.replaceAll("(?m)^\\s*$[\r\n]+", ""); // here is my regx
System.out.println(output);