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

发布于 2024-11-19 05:41:20 字数 892 浏览 5 评论 0原文

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

我的代码如下所示......请指导我如何在更新值后将 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");
             }
         }

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..

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏闹i 2024-11-26 05:41:20

像这样的东西:

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

Something like this:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
StreamResult result = new StreamResult(file);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
抹茶夏天i‖ 2024-11-26 05:41:20

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

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
    }
}

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).

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文