简体   繁体   English

Java:从XML文件重复读取的性能

[英]Java: Performance of reading repeatedly from an XML file

I am working on a project using Java and JavaFX . 我正在使用JavaJavaFX进行项目。 i do impliment a methode to allow user to change between language. 我确实暗示了一种允许用户在语言之间进行切换的方法。 But i feel that it can be slow somehow. 但是我觉得它可能会变慢。

  1. I use an XML file to store all texts in it. 我使用XML文件在其中存储所有文本。
  2. I creat the function "readXML" to read a called text from the XML. 我创建函数“ readXML”以从XML读取被调用的文本。
  3. I use this function so many times to complete the strings in the scene. 我多次使用此功能来完成场景中的字符串。

在此处输入图片说明

I think that this is wrong because i read that reading from an XML file repeatedly and successively can slow my application. 我认为这是错误的,因为我反复读取XML文件可能会减慢我的应用程序的速度。 also the XML file is getting bigger and bigger while adding other scenes. 在添加其他场景的同时,XML文件也越来越大。 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). 我仍在从事该项目,并且我的代码现在可以正常工作,但是它在每个场景更改(在500ms到1500ms之间,取决于字符串)中都显示出一点延迟。 i am afraid that this little time become bigger on next steps. 恐怕接下来的时间会变得越来越少。

Here is the class ReadXMLFile.class that contain readXML : 这里是类ReadXMLFile.class包含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) (对不起,我的英语不好,很奇怪)

That's because you are reading and parsing the whole xml file everytime you need to access a node from it. 这是因为您每次需要访问XML节点时都在读取和解析整个XML文件。

So to fix this I moved the block in readXML method that reads same file whatever parameter it takes under init method. 因此,为了解决此问题,我在readXML方法中移动了块,该块读取init方法下使用的任何参数都读取相同的文件。 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. 因此,您可以读取xml文件并一次初始化Document,然后使用相同的Document实例重复调用,而无需一遍又一遍地读取相同的文件。 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;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM