JAVA XML PARSING TO SPLIT XML based on object and class tag elements

velmanewyork

New Member
This is the JAVA XML i'm parsing..\[code\]<objects> <object>...<class>A /<class>...</object> <object>...<class>B</class>....</object> <object>...<class>A /<class>...</object></objects>\[/code\]Now i split the XML into 3 XML's based on object tag with the below code.\[code\] DocumentBuilder builder = dbf.newDocumentBuilder(); Document doc = builder.parse("xml"); doc.getDocumentElement().normalize(); TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); NodeList list =(NodeList) doc.getElementsByTagName("object"); System.out.println("XML SPLITED"); for (int i=0; i<list.getLength(); i++){ Node element = list.item(i).cloneNode(true); if(element.hasChildNodes()){ Source src = http://stackoverflow.com/questions/14051093/new DOMSource(element); FileOutputStream fs=new FileOutputStream("XML" + i + ".xml"); Result dest = new StreamResult(fs); aTransformer.transform(src, dest); fs.close(); }\[/code\]My requirement is to get only the files with class tag A.So my output will be only 2 XML's.Please post your answers.
 
Back
Top