1 21 22 package nu.xom.samples; 23 24 import nu.xom.Element; 25 import nu.xom.Node; 26 import nu.xom.ParentNode; 27 import nu.xom.Text; 28 29 38 public class TextMerger { 39 40 41 public static void merge(ParentNode parent) { 42 43 for (int i = 0; i < parent.getChildCount(); i++) { 44 Node child = parent.getChild(i); 45 if (child instanceof Text) { 46 StringBuffer sb = new StringBuffer (child.getValue()); 47 Node nextChild; 48 while ((nextChild = parent.getChild(i+1)) instanceof Text) { 49 sb.append(nextChild.getValue()); 50 parent.removeChild(nextChild); 51 if (i+1 == parent.getChildCount()) break; 52 } 53 if (sb.length() == 0) parent.removeChild(child); 54 else parent.replaceChild(child, new Text(sb.toString())); 55 } 56 else if (child instanceof Element) { 57 merge((ParentNode) child); 58 } 59 } 60 61 } 62 63 } 64 | Popular Tags |