java - Remove Element from JDOM document using removeContent() -
given following scenario, xml, geography.xml looks -
<geography xmlns:ns="some valid namespace"> <country> <region> <state> <city> <name></name> <population></population> </city> </state> </region> </country> </geography> and following sample java code -
inputstream = new fileinputstream("c:\\geography.xml"); saxbuilder saxbuilder = new saxbuilder(); document doc = saxbuilder.build(is); xpath xpath = xpath.newinstance("/*/country/region/state/city"); element el = (element) xpath.selectsinglenode(doc); boolean b = doc.removecontent(el); the removecontent() method doesn't remove element city content list of doc. value of b false
don't understand why not removing element, tried delete name & population elements xml see if issue apparently not.
way tried, don't know why know not different, still sake, use parent -
parent p = el.getparent(); boolean s = p.removecontent(new element("city")); what might problem? , possible solution? , if can share real behaviour of method removecontent(), suspect has parent-child relationship.
sure, removecontent(content child) removes child if child belongs parents immediate children, not in case. use el.detach()instead.
Comments
Post a Comment