java - How to Parse Complete XML into a string using DOM PARSER? -


for example:

if pass tagvalue=1 should return complete xml1 string me string input in function.

string input = "<1><xml1></xml1></1><2><xml2></xml2><2>.......<10000><xml10000></xml10000></10000‌​>"; string output = "<xml1></xml1>"; // tagvalue=1;

if xml has root element doable 1. parse xml using dom parser. 2. iterate through each node 3. find desired node. 4. write node in different xml using transform

sample code step 1: used xml string, can read file.

 documentbuilder dbuilder = documentbuilderfactory.newinstance()                                      .newdocumentbuilder(); inputsource =  new inputsource(new stringreader(uri));         document doc = dbuilder.parse(is); 
  1. iterate through each node

    if (doc.haschildnodes()) {

                printnote(doc.getchildnodes(), doc);          } 
  2. please put in logic iterate thorugh nodes , find right child node want process.

  3. write xml. here assumption tempnode 1 want write xml.

    transformerfactory tfactory = transformerfactory.newinstance(); transformer transformer; try { transformer = tfactory.newtransformer();

                    domsource source = new domsource(tempnode);                 streamresult result = new streamresult(system.out);                 transformer.transform(source, result); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -