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);
iterate through each node
if (doc.haschildnodes()) {
printnote(doc.getchildnodes(), doc); }
please put in logic iterate thorugh nodes , find right child node want process.
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
Post a Comment