编写(覆盖)我读过的相同XML文件

时间:2015-05-18 10:24:34

标签: java xml

我创建了一个读取XML文件的方法,但现在我需要使用相同的标记,节点和对象编写或覆盖相同的XML文件,但子节点内部的数据不同。

首先,我想让读写工作,然后我有一些想法可能将整个XML文件放入缓冲区。然后我可以将整个XML放在一个类中,然后再将该类写入相同的XML中,这只是一个想法,任何建议或想法都是受欢迎的。

我的XML文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Document Root -->
<DATA>
    <Settings USERNAME="test" PASSWORD="test" STATUS="active" / >
</DATA>

这是我的阅读代码:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Read {

    private final static String SETTINGS_LINE = Settings;

    public void readXML() {

        try {

            File xmlFile = new File("Test.xml");
            DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
            Document doc = documentBuilder.parse(xmlFile);

            // Normalize the XML file
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getDocumentElement().getChildNodes();

            for(int temp = 0; temp < nodeList.getLength(); temp++) {

                Node node = nodeList.item(temp);

                if(node instanceof Element && node.getNodeName() == SETTINGS_LINE) {

                    Element settings = (Element) node;

                    System.out.println("User" +settings.getAttribute("USERNAME"));
                    System.out.println("Password" +settings.getAttribute("PASSWORD"));
                    System.out.println("Status" +settings.getAttribute("STATUS"));


        }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

这是写作的代码不起作用:

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Modify {

    private final static String SETTINGS_LINE = "Settings";

    public static void main(String argv[]) {

       try {
        String filepath = "test.xml";
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();


        // Normalize the XML File
        doc.getDocumentElement().normalize();  

        NodeList nodeList = doc.getDocumentElement().getChildNodes();  

        for (int i = 0; i < nodeList.getLength(); i++) {

          Node node = nodeList.item(i);

              if (node instanceof Element && node.getNodeName() == SETTINGS_LINE) {  

                 Element settings = (Element) node;  

                 if("USERNAME".equals(node.getChildNodes())){
                     node.setTextContent("mivnadic");
                 }

              }

            // Write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(filepath));
            transformer.transform(source, result);

        }

        System.out.println("File saved");

       } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
       } catch (TransformerException tfe) {
        tfe.printStackTrace();
       } catch (IOException ioe) {
        ioe.printStackTrace();
       } catch (SAXException sae) {
        sae.printStackTrace();
       }
    }
}

0 个答案:

没有答案