Java:编写XML文件问题

时间:2015-08-31 04:12:47

标签: java xml dom

我正在使用Java和XML文件来处理一个应用程序,以保存语言等设置。

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>

此类应该从XML文件读取和写入。

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();
    }       
}

}

但它不会写任何东西..! 请注意,它正确读取,并且在执行时不会出现任何错误或异常。 感谢任何想法或帮助。

1 个答案:

答案 0 :(得分:1)

您的方法非常容易出错,无法扩展/自定义。如果您希望将XML读入Java代码并且可以访问您在那里设置的值,我建议您查看JAXB或像rapidxml Jackson这样的库。这将允许您将XML文件映射到Java对象并返回。例如,这将是使用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;
}

然后,您可以通过普通的Java对象访问数据。