Java: writing on XML file problems -
i working on application using java , xml file save settings language..
setting.xml
<?xml version="1.0" encoding="utf-8"?> <parametre> <param name="langue" id="1"> <val>1</val> </param> <param name="controle" id="2"> <val>0</val> </param> </parametre>
this class should read , write xml file.
import java.io.file; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.documentbuilder; import org.w3c.dom.document; import org.w3c.dom.nodelist; import org.w3c.dom.node; import org.w3c.dom.element; public class dialxmlfile { private static boolean _initialized = false; private static boolean _init = false; private static document _docx,_docy; public static void initlang() { if(_initialized) { return; } try { file fxmlfile = new file("res/files/strings.xml"); documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbuilder = dbfactory.newdocumentbuilder(); _docx = dbuilder.parse(fxmlfile); _docx.getdocumentelement().normalize(); _initialized = true; } catch (exception e) { e.printstacktrace(); } } public static void initparam() { if(_init) { return; } try { file fxmlfile = new file("res/files/setting.xml"); documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbuilder = dbfactory.newdocumentbuilder(); _docy = dbuilder.parse(fxmlfile); _docy.getdocumentelement().normalize(); _init = true; } catch (exception e) { e.printstacktrace(); } } public static string readlbl(string name, int lang) { if(!_initialized) { initlang(); } string res = ""; try { nodelist nlist = _docx.getelementsbytagname("lang"); node nnode = nlist.item(lang); if (nnode.getnodetype() == node.element_node) { element eelement = (element) nnode; res=eelement.getelementsbytagname(name).item(0).gettextcontent(); } } catch (exception e) { res="$error"; e.printstacktrace(); } return res; } public static int readctrl() { if(!_init) { initparam(); } int rs = 0; try { nodelist nparamlist = _docy.getelementsbytagname("param"); node nparamnode = nparamlist.item(1); if (nparamnode.getnodetype() == node.element_node) { element eparamelement = (element) nparamnode; rs=integer.parseint(eparamelement.getelementsbytagname("val").item(0).gettextcontent()); } } catch (exception e) { e.printstacktrace(); } return rs; } public static int readlang() { if(!_init) { initparam(); } int rs = 0; try { nodelist nparamlist = _docy.getelementsbytagname("param"); node nparamnode = nparamlist.item(0); if (nparamnode.getnodetype() == node.element_node) { element eparamelement = (element) nparamnode; rs=integer.parseint(eparamelement.getelementsbytagname("val").item(0).gettextcontent()); } } catch (exception e) { e.printstacktrace(); } return rs; } public static void writelang(string i) { initparam(); try { nodelist nparamlist = _docy.getelementsbytagname("param"); node nparamnode = nparamlist.item(0); if (nparamnode.getnodetype() == node.element_node) { element eparamelement = (element) nparamnode; eparamelement.getelementsbytagname("val").item(0).settextcontent(i); } } catch (exception e) { e.printstacktrace(); } } public static void writectrl(string i) { initparam(); try { nodelist nparamlist = _docy.getelementsbytagname("param"); node nparamnode = nparamlist.item(1); if (nparamnode.getnodetype() == node.element_node) { element eparamelement = (element) nparamnode; eparamelement.getelementsbytagname("val").item(0).settextcontent(i); } } catch (exception e) { e.printstacktrace(); } } }
but can't write anything..! note read correctly , not give error or exception when excute. ideas or help.
your approach error prone , not scalable/customizable. if looking read in xml java code , have access values set there, recommend @ jaxb or library fasterxml jackson. let map xml files java objects , back. example, start of implementation jaxb:
@xmlrootelement @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "parametre") public class parametre { list<param> params; } @xmltype(name = "param") public class param { @xmlattribute(name = "required") string name; string value; }
you have access data through normal java objects.
Comments
Post a Comment