Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java更改XML节点的值?_Java_Xml_Parsing_Xpath - Fatal编程技术网

如何使用java更改XML节点的值?

如何使用java更改XML节点的值?,java,xml,parsing,xpath,Java,Xml,Parsing,Xpath,我正在尝试重置节点的值,但由于某些原因,更改没有反映出来。我通过XPATH获取标记,但它没有设置我给出的值。 reqXML是XML文件 我的代码 public static String changeProductVersion(String reqXML) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document =

我正在尝试重置节点的值,但由于某些原因,更改没有反映出来。我通过
XPATH
获取标记,但它没有设置我给出的值。 reqXML是XML文件

我的代码

public static String changeProductVersion(String reqXML) {

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document document = null;
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource source = new InputSource();
        source.setCharacterStream(new StringReader(reqXML));
        document = builder.parse(source);

        XPath xPath = XPathFactory.newInstance().newXPath();
        Element element = (Element) xPath.evaluate(NYPG3Constants.NY_PG3_RL, document, XPathConstants.NODE);

        if(element != null) {
            element.setTextContent("17.1.0");
            System.out.println(element.getTextContent());
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return reqXML;
}

提前感谢

我不得不做一些假设和更改,因为我不知道您的XML文档是什么样子。但是,
元素
类扩展了
节点
,而
setTextContent
节点
上的一个方法。您需要更新的是第一个子节点,它通常是元素的文本值,但是您应该添加一些验证来确保。然后,更新文本值后,需要将DOM序列化回它来自的任何形式:

public static String changeProductVersion(String reqXML, String xpathExpression) {

    Document document = null;
    String updatedXML = null;

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(reqXML));
        document = builder.parse(is);

        XPath xPath = XPathFactory.newInstance().newXPath();
        Element element = (Element) xPath
                .compile(xpathExpression)
                .evaluate(document, XPathConstants.NODE);

        if(element != null) {
            NodeList childNodes = element.getChildNodes();

            // Get the first node which should be the text value.
            // Add some validation to make sure Node == Node.TEXT_NODE.
            Node node = (Node) childNodes.item(0);
            node.setTextContent("17.1.0");
        }

        System.out.println("Updated element value: " + element.getTextContent());

        // Serialise the updated DOM
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(source, result);

        updatedXML = result.getWriter().toString();

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

    System.out.println(updatedXML);
    return updatedXML;
}

XML文件的结构是什么?
NYPG3Constants.NY_PG3_RL
常量的值是什么?您只是在代码中设置节点的值,还是要将其保存在文件中?如果你想保存它,这段代码中没有关于它的说明。这是我的XPATh,我想设置17.1.0而不是14.0.0是的,它符合我的要求,非常感谢。