如何使用javax.xml.xpath更改xml标记属性中的值的内容值?

时间:2022-04-15 07:40:06

Using JAVA and the libraries of javax.xml.xpath, as XpathFactory and XpathExpression, how can I change the xml tag attribute "6" of the tag "value=6" considering that the XPath is:

使用JAVA和javax.xml.xpath的库,作为XpathFactory和XpathExpression,我如何更改标记“value = 6”的xml标记属性“6”,考虑到XPath是:

"number(/suite/test/classes/class/methods/include/parameter[1]/@value)"

Do the xPathFactory recognise xpath functions? Or have any other idea?

xPathFactory能识别xpath函数吗?还是有其他想法?

Following XML to change:

<?xml version="1.0" encoding="UTF-8"?>

<suite name="Test1" Alowinterrupt="true">
    <test name="testscale">
        <classes>
            <class name="test">
                <methods>
                <include name="poweronoff">
                <parameter name="ScaleTesting" value="6"/>
                </include>
                </methods>
            </class>
        </classes>
    </test>
</suite>

1 个解决方案

#1


0  

You can modify XML File in Java using DOM parser.

您可以使用DOM解析器在Java中修改XML文件。

// parse the XML as a W3C Document
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
        .newDocumentBuilder();
Document document = builder.parse(new File(xmlFilePath));

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/suite/test/classes/class/methods/include/parameter[1]";
Node parameter = (Node) xpath.evaluate(expression, document,
        XPathConstants.NODE);

// update parameter , set the value to 10
NamedNodeMap attribute = parameter.getAttributes();
Node nodeAttr = attribute.getNamedItem("value");

System.out.println("Old id value: " + nodeAttr.getTextContent());
Transformer transformer = TransformerFactory.newInstance()
        .newTransformer();
Result output = new StreamResult(new File(xmlFilePath));
nodeAttr.setTextContent("2");
Source input = new DOMSource(document);
transformer.transform(input, output);
System.out.println("New id value: " + nodeAttr.getTextContent());

Output:

Old parameter value: 6
New parameter value: 2

#1


0  

You can modify XML File in Java using DOM parser.

您可以使用DOM解析器在Java中修改XML文件。

// parse the XML as a W3C Document
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
        .newDocumentBuilder();
Document document = builder.parse(new File(xmlFilePath));

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/suite/test/classes/class/methods/include/parameter[1]";
Node parameter = (Node) xpath.evaluate(expression, document,
        XPathConstants.NODE);

// update parameter , set the value to 10
NamedNodeMap attribute = parameter.getAttributes();
Node nodeAttr = attribute.getNamedItem("value");

System.out.println("Old id value: " + nodeAttr.getTextContent());
Transformer transformer = TransformerFactory.newInstance()
        .newTransformer();
Result output = new StreamResult(new File(xmlFilePath));
nodeAttr.setTextContent("2");
Source input = new DOMSource(document);
transformer.transform(input, output);
System.out.println("New id value: " + nodeAttr.getTextContent());

Output:

Old parameter value: 6
New parameter value: 2