Robert Strauch
Robert Strauch

Reputation: 12896

Finding an XML node by its attribute value and updating in Java

Let's say I have the following XML document:

<Offices>
  <Office name="P">
    <Counter>1000</Counter>
  </Office>
  <Office name="K">
    <Counter>1006</Counter>
  </Office>
</Offices>

With that document I need to perform the following in Java:

  1. Parse the XML.
  2. Get the value of Counter given a certain value for a name attribute.
  3. Update the XML with a new value for Counter for exactly this Office.

For 2. I have considered using XPath but editing/updating the XML seems to be not that easy this way.

How could I go through the XML finding a certain office name and update its counter? The XML itself won't be large, only something like 20 office entries max.

Upvotes: 0

Views: 90

Answers (1)

MMendes
MMendes

Reputation: 84

You can try looking at this answer: https://stackoverflow.com/a/5059411/1571550

It seems pretty straightforward and generic solution.

Upvotes: 1

Related Questions