JAVA,如何在xml中编辑节点内的文本?

时间:2019-04-02 12:45:10

标签: java xml

所以说我有当前的XML文件。

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <apps>
        <app name="app1">
            <url>someUrl</url>
            <username>user1</username>
            <password>qwerty123</password>
        </app>
        <app name="app2">
            <url>someUrl</url>
            <username>user2</username>
            <password>asdasdasd</password>
        </app>
        <app name="app3">
            <url>someUrl</url>
            <username>user3</username>
            <password>123456789</password>
        </app>
    </apps>
</config>

我已经广泛搜索如何将第一个密码(qwerty123)编辑为其他密码,然后保存文件,但是我找不到合适的解决方案。

这里有人对我该怎么做吗?

2 个答案:

答案 0 :(得分:1)

使用DOM解析器的示例解决方案:

        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<config>\n" +
                "    <apps>\n" +
                "        <app name=\"app1\">\n" +
                "            <url>someUrl</url>\n" +
                "            <username>user1</username>\n" +
                "            <password>qwerty123</password>\n" +
                "        </app>\n" +
                "        <app name=\"app2\">\n" +
                "            <url>someUrl</url>\n" +
                "            <username>user2</username>\n" +
                "            <password>asdasdasd</password>\n" +
                "        </app>\n" +
                "        <app name=\"app3\">\n" +
                "            <url>someUrl</url>\n" +
                "            <username>user3</username>\n" +
                "            <password>123456789</password>\n" +
                "        </app>\n" +
                "    </apps>\n" +
                "</config>";
//here use your InputStream for example from file
        InputStream inputStream = new ByteArrayInputStream(xml.getBytes());


        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(inputStream);
        doc.getDocumentElement().normalize();

        doc.getElementsByTagName("password").item(0).getFirstChild().setNodeValue("new content");
//here use your outputStream for example file output stream
        OutputStream outputStream = new ByteArrayOutputStream();
        Source xmlSource = new DOMSource(doc);
        Result outputTarget = new StreamResult(outputStream);
        TransformerFactory.newInstance().newTransformer()
          .transform(xmlSource, outputTarget);
//when you replace output stream with file output stream remove this, as this line is only for debugging 
        System.out.println(new String(((ByteArrayOutputStream) outputStream).toByteArray()));

答案 1 :(得分:0)

也许您可以这样做:

        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + 
            "<config>\r\n" + 
            "    <apps>\r\n" + 
            "        <app name=\"app1\">\r\n" + 
            "            <url>someUrl</url>\r\n" + 
            "            <username>user1</username>\r\n" + 
            "            <password>qwerty123</password>\r\n" + 
            "        </app>\r\n" + 
            "        <app name=\"app2\">\r\n" + 
            "            <url>someUrl</url>\r\n" + 
            "            <username>user2</username>\r\n" + 
            "            <password>asdasdasd</password>\r\n" + 
            "        </app>\r\n" + 
            "        <app name=\"app3\">\r\n" + 
            "            <url>someUrl</url>\r\n" + 
            "            <username>user3</username>\r\n" + 
            "            <password>123456789</password>\r\n" + 
            "        </app>\r\n" + 
            "    </apps>\r\n" + 
            "</config>";

    try {

        SOAPMessage message = MessageFactory.newInstance().createMessage(null,new ByteArrayInputStream(xml.getBytes()));
        System.out.println(message.getSOAPPart().getElementsByTagName("password").item(0).getTextContent());
        message.getSOAPPart().getElementsByTagName("password").item(0).setTextContent("NewPassword");
        System.out.println(message.getSOAPPart().getElementsByTagName("password").item(0).getTextContent());
        StringWriter messageString = new StringWriter();
        TransformerFactory.newInstance().newTransformer().transform(
        new DOMSource(message.getSOAPPart()),
        new StreamResult(messageString));
        BufferedWriter writer = null;
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("file.xml", true), "utf-8"));
        writer.write(messageString.toString());
        writer.newLine();
        writer.close();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

或者,如果需要,您可以只使用Marshaller和Unmarshaller。