开发者

How do I edit/update a node in an XML file using xpath in Java

开发者 https://www.devze.com 2023-03-16 04:31 出处:网络
Java Code: public void update(String id) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

Java Code:

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, XPath开发者_JS百科Constants.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 BEFORE

<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>

XML AFTER

<Servers>
    <server ID="12234">  // <-- This guy is now changed
        <name>SomethingElse</name>
        <ip>localHost</ip>
        <port>4447</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>


Probably not best-effective code, but it works (avoiding text elements).

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") ;
}

Output changed XML fragment:

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


You can try this:

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


Take a look at Node#getFirstChild() and Node#getNextSibling() to iterate over child nodes. Also remember that text nodes are present between elements as well and you have to ignore them if you don't want them.

0

精彩评论

暂无评论...
验证码 换一张
取 消