java - How to process all nodes in a Nodelist -
i trying process nodes in node list , replace special characters in text.
the issue face need return object contains values replaced. if use recursion not possible return value right?
how can iterate xml nodelist processing elements , removing special characters? apporach @ moment this
public static ixmldatagram removeillegalcharsfromdatagram(ixmldatagram dg, string[] illegalvalues) { nodelist nodelist = dg.getasdomelement().getelementsbytagname("*"); (int = 0 ; < nodelist.getlength() ; i++) { node node = nodelist.item(i); if (node.getnodetype() == node.element_node) { for(string replacechar : illegalvalues) { // current element if(node.gettextcontent().contains(replacechar)) { node.settextcontent(node.gettextcontent().replace(replacechar, "")); } } } } return dg; }
but doesn't take account of child nodes change regularly in these documents , nested quite deep
thanks
a recursion simplest approach deep processing of doms:
public static void removeillegalcharsfromdatagram(ixmldatagram dg, string[] illegalvalues) { node root = dg.getasdomelement().getdocumentelement(); removeillegal(illegalvalues, root); } private static void removeillegal(string[] illegalvalues, node root) { nodelist nodelist = root.getchildnodes(); (int = 0; < nodelist.getlength(); i++) { node node = nodelist.item(i); removeillegal(illegalvalues,node);//<-- call every child if (node.getnodetype() == node.element_node) { (string replacechar : illegalvalues) { // current element if (node.gettextcontent().contains(replacechar)) { node.settextcontent(node.gettextcontent().replace( replacechar, "")); } } } } }
Comments
Post a Comment