Retrieve XMl Data Using Java -
i new java , xml parsing. requirement take xml file , store xml file's data in database in table , columns format using java. tried in google right solution it. helpless. till did is, can xml data dynamically , store either tag names or values. req take tag names once column names , data related particular column in row format can please correct code.
<?xml version="1.0"?> <company> <staff> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>100000</salary> </staff> <staff> <firstname>low</firstname> <lastname>yin fong</lastname> <nickname>fong fong</nickname> <salary>200000</salary> </staff> </company>
java code
import java.io.*; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.*; public class xmldata{ static public void main(string[] arg){ try { bufferedreader bf = new bufferedreader(new inputstreamreader(system.in)); system.out.print("enter xml file path: "); string xmlfile = bf.readline(); //store string file file file = new file(xmlfile); if(file.exists()){ // create factory documentbuilderfactory factory =documentbuilderfactory.newinstance(); // use factory create builder documentbuilder builder = factory.newdocumentbuilder(); document doc = builder.parse(xmlfile); element docele = doc.getdocumentelement(); system.out.println("root element of document: "+ docele.getnodename()); // list of elements in document nodelist list = doc.getelementsbytagname("*"); int totalelements = list.getlength(); system.out.println("xml elements: " + totalelements); (int i=0; i<list.getlength(); i++) { // element element element = (element)list.item(i); string tag=element.gettagname(); string name=list.item(0).getchildnodes().item(0).getnodevalue(); system.out.println(name); system.out.print(tag); system.out.print(" "); //system.out.println(element.getnodename()); } } else{ system.out.print("file not found!"); } } catch (exception e) { system.exit(1); } } }
output code:
company staff firstname lastname nickname salary staff firstname lastname nickname salary
expected output:
firstname, lastname, nickname , salary //column yong ,mook kim, mkyong , 100000 // rows low ,yin fong, fong fong ,200000
i'm assuming have created database table 4 columns - firstname, lastname, nickname, salary.
it's straight forward read data , store in db.
import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import org.w3c.dom.document; import org.w3c.dom.element; import org.w3c.dom.nodelist; public class parsestaff { public static void main(string [] args){ parsefile(); } public static void parsefile() { //get factory documentbuilderfactory dbf = documentbuilderfactory.newinstance(); try { //using factory instance of document builder documentbuilder db = dbf.newdocumentbuilder(); //parse using builder dom representation of xml file //document dom = db.parse("employees.xml"); document dom = db.parse("c:\\gae\\netbeansprojects\\test\\src\\statff.xml"); //get root element element docele = dom.getdocumentelement(); //get nodelist of elements nodelist nl = docele.getelementsbytagname("staff"); if (nl != null && nl.getlength() > 0) { (int = 0; < nl.getlength(); i++) { //get employee element element el = (element) nl.item(i); string firstname = gettextvalue(el, "firstname"); string lastname = gettextvalue(el, "lastname"); string nickname = gettextvalue(el, "nickname"); int salary = getintvalue(el, "salary"); system.out.println(firstname); system.out.println(lastname); system.out.println(nickname); system.out.println(salary); } } } catch (exception e) { e.printstacktrace(); } } private static string gettextvalue(element ele, string tagname) { string textval = null; nodelist nl = ele.getelementsbytagname(tagname); if (nl != null && nl.getlength() > 0) { element el = (element) nl.item(0); textval = el.getfirstchild().getnodevalue(); } return textval; } private static int getintvalue(element ele, string tagname) { return integer.parseint(gettextvalue(ele, tagname)); } }
will print:
yong mook kim mkyong 100000 low yin fong fong fong 200000
Comments
Post a Comment