最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Get rid of empty line in XML created by call of JOOX' remove() - Stack Overflow

programmeradmin3浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论