java - Covert database table result into XML - How to generate an XML document -


i have query when run returns resultset following data in order , grouping shown:

country     region      town --------------------------------------- england     north       newcastle england     north       manchester england     north       leeds england     south       london england     south       bristol england     south       birmingham england     south       portsmouth norway      north       trondheim norway      north       tromso norway      south       oslo norway      south       stavanger norway      west        bergen 

using java, convert returned result xml document shown below:

<countries>     <country>         <countryname>england</countryname>         <region name = "south">             <town>london</town>             <town>bristol</town>             <town>birmingham</town>             <town>portsmouth</town>         </region>         <region name = "north">             <town>newcastle</town>             <town>leeds</town>         </region>     <country>         <country>         <countryname>norway</countryname>         <region name = "south">             <town>oslo</town>             <town>stavanger</town>         </region>         <region name = "west">             <town>bergen</town>         </region>         <region name = "north">             <town>trondheim</town>             <town>tromso</town>         </region>            <country> <countries> 

what best way traverse data tags created , closed @ correct position? have seen example here http://www.mkyong.com/java/how-to-create-xml-file-in-java-jdom-parser/ structure of data flat unlike sample using require multiple loops.

check code once changing attributes mentioned @ particular location

package com.annexure.main;

    import java.io.file;     import java.io.filewriter;     import java.nio.file.filealreadyexistsexception;     import java.sql.connection;     import java.sql.drivermanager;     import java.sql.resultset;     import java.sql.statement;      import org.w3c.dom.document;     import org.w3c.dom.element;      import com.sun.org.apache.xerces.internal.dom.documentimpl;     import com.sun.org.apache.xml.internal.serialize.outputformat;     import com.sun.org.apache.xml.internal.serialize.xmlserializer;     /* jdbc classes*/     /* java io */     /* w3c interfaces */     /* xerces dom classes */     /* xerces serializer */       public class xmlmain {         public static final string jdbcurl = "oracle.jdbc.driver.oracledriver";        public static final string jdbcdriver ="jdbc:oracle:thin:@localhost:1521:xe";        public static final string sql = "select empid, empname, role employee";        public static string outputfile = "d:employee.xml";  //replace file country.xml        public static void main(string[] args) {           try{           /** step 1 : making jdbc connection database" **/          class.forname(jdbcurl) ;         connection conn = drivermanager.getconnection(jdbcdriver,"system","root");           /** step 2 : retrieve customer data database **/          statement statement = conn.createstatement();          resultset employeers = statement.executequery(sql);           /** step 3 : build customer xml dom **/          document xmldoc = buildemployeexml(employeers);          /** step 4 : write output file **/          file outputfile = new file(outputfile);          printdom(xmldoc, outputfile);           conn.close(); /*connection close*/          } catch(filealreadyexistsexception f){             system.out.println("file alread present @ location");         }         catch(exception e)          {            system.out.println("really poor exception handling " +e.tostring());          }       }//main         /*build xml document database. xml object returned main method written flat file.*/        private static document buildemployeexml(resultset _employeers) throws exception        {         document xmldoc = new documentimpl();         /* creating root element */  //replace employeetable countries set countries tag       element rootelement = xmldoc.createelement("employeetable");        xmldoc.appendchild(rootelement);         while(_employeers.next())         {           element emp = xmldoc.createelement("employee"); //replace employee country country tag          /* build customerid attribute*/          emp.setattribute("empid", _employeers.getstring("empid"));           /* creating elements within customer dom*/          element empname = xmldoc.createelement("empname");          element role = xmldoc.createelement("role");           /* populating customer dom data*/          empname.appendchild(xmldoc.createtextnode(_employeers.getstring("empname")));          role.appendchild(xmldoc.createtextnode(_employeers.getstring("role")));           /* adding empname , role elements employee element*/          emp.appendchild(empname);          emp.appendchild(role);           /* appending emp root class*/          rootelement.appendchild(emp);         }        return xmldoc;        }         /* printdom write contents of xml document passed onto out file*/        private static void printdom(document _xmldoc, file _outputfile) throws exception        {          outputformat outputformat = new outputformat("xml","utf-8",true);          filewriter filewriter = new filewriter(_outputfile);           xmlserializer xmlserializer = new xmlserializer(filewriter, outputformat);           xmlserializer.asdomserializer();           xmlserializer.serialize(_xmldoc.getdocumentelement());        }        }  

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -