Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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中使用xpath编辑/更新XML文件中的节点_Java_Xml_Xpath - Fatal编程技术网

如何在Java中使用xpath编辑/更新XML文件中的节点

如何在Java中使用xpath编辑/更新XML文件中的节点,java,xml,xpath,Java,Xml,Xpath,Java代码: public void update(String id) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(file_); XPathFactor

Java代码:

public void update(String id) throws Exception
  {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(file_);
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();

    XPathExpression expr = xpath.compile("Servers/server[@ID=" + id + "]");
    Node nodeGettingChanged = (Node) expr.evaluate(doc, XPathConstants.NODE);

   //HELP START

           //? ? ? How do get the node/elements guts to alter that guy

   //HELP END

   TransformerFactory transformerFactory = TransformerFactory.newInstance();
   Transformer transformer = transformerFactory.newTransformer();
   DOMSource source = new DOMSource(doc);

   StreamResult result = new StreamResult(file_);
   transformer.transform(source, result);
  }
之前的XML

<Servers>
    <server ID="12234">  // <-- I want to change this node
        <name>Greg</name>
        <ip>127.0.0.1</ip>
        <port>1897</port>
    </server>
    <server ID="42234">
        <name>Bob</name>
        <ip>127.0.0.1</ip>
        <port>1898</port>
    </server>
    <server ID="5634">
        <name>Tom</name>
        <ip>127.0.0.1</ip>
        <port>1497</port>
    </server>
</Servers>

//您可以尝试以下方法:

NodeList children = nodeGettingChanged.getChildNodes();
chidren.item(0).setNodeValue("SomethingElse");
chidren.item(1).setNodeValue("localHost");
chidren.item(2).setNodeValue("4447");

查看
Node#getFirstChild()
Node#getNextSibling()
以迭代子节点。还请记住,元素之间也存在文本节点,如果不需要,则必须忽略它们。

可能不是最有效的代码,但它可以工作(避免使用文本元素)

输出更改的XML片段:

<server ID="12234">
    <name>SomethingElse</name>
    <ip>localHost</ip>
    <port>4447</port>
</server>

某物
本地主机
4447
NodeList childNodes = nodeGettingChanged.getChildNodes();
for (int i = 0; i != childNodes.getLength(); ++i)
{
    Node child = childNodes.item(i);
    if (!(child instanceof Element))
        continue;

    if (child.getNodeName().equals("name"))
        child.getFirstChild().setNodeValue("SomethingElse") ;
    else if (child.getNodeName().equals("ip"))
        child.getFirstChild().setNodeValue("localHost") ;
    else if (child.getNodeName().equals("port"))
        child.getFirstChild().setNodeValue("4447") ;
}
<server ID="12234">
    <name>SomethingElse</name>
    <ip>localHost</ip>
    <port>4447</port>
</server>