Skip to content

Commit 9aa16ad

Browse files
CU-86b2x4qmn_GH-133--how-to-remove-empty-xml-tags (#152)
1 parent 04e4d22 commit 9aa16ad

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### 9.5.7 - SNAPSHOT
44
* Enhanced the MX parser log verbosity when parsing malformed content
5+
* Added new logic to MxNode to removing all leaves with empty attributes and empty content
56

67
#### 9.5.6 - January 2025
78
* Changed the `MxParseUtils` findByTags and findByPath methods to return the element values instead of the XML stream object

iso20022-core/src/main/java/com/prowidesoftware/swift/model/MxNode.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,35 @@ public String path() {
299299
return this.parent.path() + PATH_SEPARATOR + this.localName;
300300
}
301301
}
302+
303+
/**
304+
* Traverses the XML tree from this node down, removing all leaves with empty attributes and empty content.
305+
*
306+
* @since 9.5.7
307+
*/
308+
public void removeEmptyElements() {
309+
if (this.children != null && !this.children.isEmpty()) {
310+
List<MxNode> removables = new ArrayList<>();
311+
for (MxNode child : this.children) {
312+
if (child.isEmpty()) {
313+
removables.add(child);
314+
} else {
315+
child.removeEmptyElements();
316+
}
317+
}
318+
for (MxNode removable : removables) {
319+
this.children.remove(removable);
320+
}
321+
}
322+
}
323+
324+
/**
325+
* @return true if this node has no value, no attributes and no children
326+
* @since 9.5.7
327+
*/
328+
public boolean isEmpty() {
329+
return StringUtils.isEmpty(this.value)
330+
&& (this.attributes == null || this.attributes.isEmpty())
331+
&& (this.children == null || this.children.isEmpty());
332+
}
302333
}

iso20022-core/src/test/java/com/prowidesoftware/swift/model/MxNodeTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,60 @@ public void testXxeDisabled() {
413413
final MxNode doc = MxNode.parse(xml);
414414
assertNull(doc);
415415
}
416+
417+
@Test
418+
public void testRemoveEmptyElements() {
419+
final MxNode n = new MxNode(null, "root");
420+
new MxNode(n, "X").setValue("value"); // Add child with value
421+
assertEquals(1, n.getChildren().size());
422+
423+
n.removeEmptyElements(); // Should not remove the child as it has value
424+
assertFalse(n.getChildren().isEmpty()); // Still has children
425+
}
426+
427+
@Test
428+
public void testRemoveEmptyElements2() {
429+
final MxNode n = new MxNode(null, "root");
430+
MxNode x = new MxNode(n, "X");
431+
x.addAttribute("a", "attr-value"); // Add an attribute
432+
assertEquals(1, n.getChildren().size());
433+
434+
n.removeEmptyElements(); // Should not remove node as it has an attribute
435+
assertFalse(n.getChildren().isEmpty());
436+
}
437+
438+
@Test
439+
public void testRemoveEmptyElements3() {
440+
final MxNode n = new MxNode(null, "root");
441+
new MxNode(n, "X"); // Create child node with no value and no attributes
442+
assertEquals(1, n.getChildren().size());
443+
444+
n.removeEmptyElements(); // Should remove the child as it's empty
445+
assertTrue(n.getChildren().isEmpty()); // No children left
446+
}
447+
448+
@Test
449+
public void testRemoveEmptyElements4() {
450+
final MxNode n = new MxNode(null, "root");
451+
MxNode x = new MxNode(n, "X");
452+
new MxNode(x, "Y"); // Create an empty child under X
453+
454+
n.removeEmptyElements(); // Should remove Y as it's empty
455+
assertEquals(1, n.getChildren().size()); // X should still exist
456+
assertTrue(n.getChildren().get(0).getChildren().isEmpty()); // X should have no children
457+
}
458+
459+
@Test
460+
public void testRemoveEmptyElements5() {
461+
final MxNode n = new MxNode(null, "root");
462+
MxNode x = new MxNode(n, "X");
463+
new MxNode(x, "Y").setValue("y"); // Add child with value
464+
new MxNode(x, "Z").setValue(""); // Add child with empty value
465+
466+
assertEquals(1, n.getChildren().size());
467+
assertEquals(2, x.getChildren().size());
468+
469+
n.removeEmptyElements(); // Should remove Z as it's empty
470+
assertEquals(1, x.getChildren().size()); // Only Y should remain
471+
}
416472
}

0 commit comments

Comments
 (0)