java - Read XML file using DocumentBuilder.parse() method. File is missing -
i have annoying problem. looking resolve on google , stackoverflow, trying fix tried lot of solutions , still not working.
i want read xml data xml file using documentbuilder.
this code:
public void loadfromfile(){ try{ //file fxmlfile = new file("fishlist.xml"); documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbuilder = dbfactory.newdocumentbuilder(); document doc = dbuilder.parse("file:///c:/fishlist.xml"); // <--- there exception run doc.getdocumentelement().normalize(); }catch(exception ex){ ex.printstacktrace(); } }
and when i'm trying run method, shows me exception:
w/system.err: java.io.filenotfoundexception: /c:/fishlist.xml: open failed: enoent (no such file or directory) w/system.err: @ libcore.io.iobridge.open(iobridge.java:456) w/system.err: @ java.io.fileinputstream.<init>(fileinputstream.java:76) w/system.err: @ libcore.net.url.fileurlconnection.connect(fileurlconnection.java:123) w/system.err: @ org.apache.harmony.xml.parsers.documentbuilderimpl.parse(documentbuilderimpl.jav a:117) w/system.err: @ javax.xml.parsers.documentbuilder.parse(documentbuilder.java:155) w/system.err: caused by: android.system.errnoexception: open failed: enoent (no such file or directory) w/system.err: @ libcore.io.posix.open(native method) w/system.err: @ libcore.io.blockguardos.open(blockguardos.java:186) w/system.err: @ libcore.io.iobridge.open(iobridge.java:442)
and yes. have file in direction. please help.
data read
<?xml version="1.0"?> <company> <staff id="1001"> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>100000</salary> </staff> <staff id="2001"> <firstname>low</firstname> <lastname>yin fong</lastname> <nickname>fong fong</nickname> <salary>200000</salary> </staff> </company>
try out java code working well
try { file fxmlfile = new file("/users/mkyong/staff.xml"); documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbuilder = dbfactory.newdocumentbuilder(); document doc = dbuilder.parse(fxmlfile); //optional, recommended //read - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work doc.getdocumentelement().normalize(); system.out.println("root element :" + doc.getdocumentelement().getnodename()); nodelist nlist = doc.getelementsbytagname("staff"); system.out.println("----------------------------"); (int temp = 0; temp < nlist.getlength(); temp++) { node nnode = nlist.item(temp); system.out.println("\ncurrent element :" + nnode.getnodename()); if (nnode.getnodetype() == node.element_node) { element eelement = (element) nnode; system.out.println("staff id : " + eelement.getattribute("id")); system.out.println("first name : " + eelement.getelementsbytagname("firstname").item(0).gettextcontent()); system.out.println("last name : " + eelement.getelementsbytagname("lastname").item(0).gettextcontent()); system.out.println("nick name : " + eelement.getelementsbytagname("nickname").item(0).gettextcontent()); system.out.println("salary : " + eelement.getelementsbytagname("salary").item(0).gettextcontent()); } } } catch (exception e) { e.printstacktrace(); }
root element :company
current element :staff staff id : 1001 first name : yong last name : mook kim nick name : mkyong salary : 100000 current element :staff staff id : 2001 first name : low last name : yin fong nick name : fong fong salary : 200000
Comments
Post a Comment