java - Remove Parent Keep Childs- DOM -
this xml input
<?xml version="1.0" encoding="utf-8" standalone="no"?> <ns0:emessage xmlns:ns0="http://www.abc/something"> <ns0:header> <ns0:snumber>1613</ns0:snumber> </ns0:header> <ns0:name>someevent</ns0:name> <ns0:namespace>http://www.abc/something.xsd</ns0:namespace> <ns0:id>3</ns0:id> <ns0:myproperty> <ns0:name>extid</ns0:name> <ns0:value>test_id_12</ns0:value> </ns0:myproperty> <ns0:myproperty> <ns0:name>cversion</ns0:name> <ns0:value>0</ns0:value> </ns0:myproperty> </ns0:emessage>
convert
<?xml version="1.0" encoding="utf-8" standalone="no"?> <ns0:emessage xmlns:ns0="http://www.abc/something"> <ns0:header> <ns0:snumber>1613</ns0:snumber> </ns0:header> <ns0:name>someevent</ns0:name> <ns0:namespace>http://www.abc/something.xsd</ns0:namespace> <ns0:id>3</ns0:id> <extid>test_id_12</extid> <cversion>0</cversion> </ns0:emessage>
basically needed parent tag should removed , childs should immideate child of root.
logic part
node root = doc.getelementsbytagname("ns0:emessage").item(0); nodelist listofnodes = root.getchildnodes(); int length_child_nodes=listofnodes.getlength(); system.out.println("length of child nodes root element"+length_child_nodes); (int = 0; < listofnodes.getlength(); i++) { // node picked in click , element , have childs. node listitem = listofnodes.item(i); node fchild=listitem.getfirstchild(); string value=""; if ("ns0:name".equals(fchild.getnodename())) { node oldchildnodename=listitem.getfirstchild(); node value=fchild.getnextsibling(); if("ns0:value".equals(fchild.getnextsibling().getnodename())){ value=fchild.getnextsibling().gettextcontent(); } string localname=listitem.getfirstchild().gettextcontent(); element newchildnode = doc.createelement(localname); // replace existing node property name. listitem.replacechild(newchildnode, oldchildnodeprpertyname); // replace existing node property value listitem.removechild(value); //system.out.println("remove child"+node.removechild(oldchildnodepropertyvalue)); newchildnode.settextcontent(value); //system.out.println("new node name::"+newchildnode.getname()); //system.out.println("old node::"+oldchildnodeprpertyname.getname()); //system.out.println("newvalue::"+newchildnode.gettextcontent()); } }
the above code manipulation required.but parent of childs tag needed removed.which not getting through.and ideas great me.
Comments
Post a Comment