Reputation: 856
I am working on a project using Java and JavaFX. i do impliment a methode to allow user to change between language. But i feel that it can be slow somehow.
I think that this is wrong because i read that reading from an XML file repeatedly and successively can slow my application. also the XML file is getting bigger and bigger while adding other scenes. that why i need help in this. I still working on the project and my codes work right fine rigth now but it show a little slowing in every scene change (between 500ms and 1500ms depending on strings). i am afraid that this little time become bigger on next steps.
Here is the class ReadXMLFile.class
that contain readXML
:
package modele;
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;
import java.io.File;
/**
* Créer par Malek Boubakri le 27/07/2015 à 20:37.
*/
public class ReadXMLFile {
public static String readXML(String name,int lang) {
String res = "";
try {
File fXmlFile = new File("res/files/strings.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.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) {
e.printStackTrace();
}
return res;
}
}
and this is how i use it:
....
lbl_elev_opt1.setText(ReadXMLFile.readXML("lbl_elev_list",SettingDialer.langID));
lbl_elev_opt2.setText(ReadXMLFile.readXML("lbl_elev_edit",SettingDialer.langID));
lbl_elev_opt3.setText(ReadXMLFile.readXML("lbl_elev_add",SettingDialer.langID));
lbl_elev_opt4.setText(ReadXMLFile.readXML("lbl_elev_del",SettingDialer.langID));
lbl_ens_opt1.setText(ReadXMLFile.readXML("lbl_ens_list",SettingDialer.langID));
lbl_ens_opt2.setText(ReadXMLFile.readXML("lbl_ens_edit",SettingDialer.langID));
lbl_ens_opt3.setText(ReadXMLFile.readXML("lbl_ens_class",SettingDialer.langID));
lbl_ens_opt4.setText(ReadXMLFile.readXML("lbl_ens_exam",SettingDialer.langID));
lbl_cal_opt1.setText(ReadXMLFile.readXML("lbl_cal_in",SettingDialer.langID));
lbl_cal_opt2.setText(ReadXMLFile.readXML("lbl_cal_add",SettingDialer.langID));
lbl_cal_opt3.setText(ReadXMLFile.readXML("lbl_cal_edit",SettingDialer.langID));
lbl_arch_opt1.setText(ReadXMLFile.readXML("lbl_arch_rech",SettingDialer.langID));
lbl_arch_opt2.setText(ReadXMLFile.readXML("lbl_arch_add",SettingDialer.langID));
lbl_arch_opt3.setText(ReadXMLFile.readXML("lbl_arch_edit",SettingDialer.langID));
lbl_doc_opt1.setText(ReadXMLFile.readXML("lbl_doc_off",SettingDialer.langID));
lbl_doc_opt2.setText(ReadXMLFile.readXML("lbl_doc_dip",SettingDialer.langID));
lbl_doc_opt3.setText(ReadXMLFile.readXML("lbl_doc_aut",SettingDialer.langID));
at lease 100 other uses....
Please if anything is blur just comment! waiting for your help..and thanks..
(Sorry for my bad, strange english)
Upvotes: 0
Views: 1418
Reputation: 599
That's because you are reading and parsing the whole xml file everytime you need to access a node from it.
So to fix this I moved the block in readXML method that reads same file whatever parameter it takes under init method. So you read xml file and initialize Document for once and use same Document instance for repeating calls without reading same file over and over again. You can just replace your code with below without any changes in other classes.
public class ReadXMLFile {
private static boolean _initialized = false;
private static Document _doc;
public static void init() {
if(_initialized) {
return;
}
try {
File fXmlFile = new File("res/files/strings.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
_doc = dBuilder.parse(fXmlFile);
_doc.getDocumentElement().normalize();
_initialized = true;
} catch (Exception e) {
e.printStackTrace();
}
}
public static String readXML(String name, int lang) {
if(!_initialized) {
init();
}
String res = "";
try {
NodeList nList = _doc.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) {
e.printStackTrace();
}
return res;
}
}
Upvotes: 1