如何在xml文件中保存和更新值?

时间:2021-09-16 12:15:43

I am reading a xml file from the SD card. Here I want to change the values of the XML file and I want to save the file to the sd card..

我正在从SD卡读取xml文件。在这里,我想更改XML文件的值,我想将文件保存到SD卡。

My code is like below.... Please guide me how to save XML file to the sd card after updating the values..

我的代码如下所示....请指导我在更新值后如何将XML文件保存到SD卡。

public void modifyNodeval(){
        try{
         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
         Document doc = docBuilder.parse(new File("/sdcard/sss.xml"));

        //Get the staff element by tag name directly
         Node nodes = doc.getElementsByTagName("Employee").item(0);
        //loop the staff child node
         NodeList list = nodes.getChildNodes();

         for (int i =0; i<list.getLength();i++){
             Node node = list.item(i);

             //get the salary element, and update the value
             if("Emp_Name".equals(node.getNodeName())){
                 node.setNodeValue("795796");
             }
         }

3 个解决方案

#1


1  

Something like this:

像这样的东西:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
StreamResult result = new StreamResult(file);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);

#2


0  

how to modify xml node value?

如何修改xml节点值?

#3


0  

This method writes a DOM document to a file on the SD Card If you want to test this in the emulator, make sure your AVD image is setup with an SD Card image (done during image creation).

此方法将DOM文档写入SD卡上的文件如果要在模拟器中对其进行测试,请确保使用SD卡映像设置AVD映像(在映像创建期间完成)。

public static void writeXmlFile(Document doc, String filename) {
    try {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        File file new File(Environment.getExternalStorageDirectory(),fileName);

        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
         // handle exception
    } catch (TransformerException e) {
         // handle exception
    }
}

#1


1  

Something like this:

像这样的东西:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
StreamResult result = new StreamResult(file);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);

#2


0  

how to modify xml node value?

如何修改xml节点值?

#3


0  

This method writes a DOM document to a file on the SD Card If you want to test this in the emulator, make sure your AVD image is setup with an SD Card image (done during image creation).

此方法将DOM文档写入SD卡上的文件如果要在模拟器中对其进行测试,请确保使用SD卡映像设置AVD映像(在映像创建期间完成)。

public static void writeXmlFile(Document doc, String filename) {
    try {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        File file new File(Environment.getExternalStorageDirectory(),fileName);

        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
         // handle exception
    } catch (TransformerException e) {
         // handle exception
    }
}