Confused on updating correct db in xml file

I have an xml file. The xml file contains different db's but I want to use- db1 , db2 , db3.

I have the code in a xmlwrite.java file to update the existing tags in the xml file where the 3 db's are. I have a method in the xmlwrite class that passes through document doc and MainEnvironment environment (MainEnvironment is a class) (document doc is used as part of documentbuilder)

The MainEnvironment.java file has the 'envrionmentID' in it - it's declared globally and is get and set.

Currently I am getting it like:

then I proceed to use code that will update existing elements in the xml file (I have a few of these to update diff elements):

then I also have the code to update the file:

Because I have 3 different db's I want to use I need to specify the environmentID so that it will update the correct db. I need it to sort of update the other two as well even though no new data goes into them.

Below is an example of what the xml file looks like:

I managed to read it fine just updating the file I'm not 100% on due to the environment id.

I'm not at all clear on what your problem is with the "environment id". So let me just suggest that if you have a node which you believe is an element node, you can cast it to the Element type. This type has methods for working with the element's attributes (which is what "id" is with respect to "environment" (or "Environment", whichever it really is)). So if your problem was knowing what the value of the "id" was, that's how you would do it.

William Brogden

Author and all-around good cowpoke
Rancher

Posts: 13077

6

posted 2 years ago

The "Db" Element has an Attribute named "id" - right?

So if you have a "Db" Element you can use the getAttribute("id") method in the Element interface to get the String value.

See the org.w3c.dom JavaDocs for details of what you can do with an Element.

See the org.w3c.dom.Node Javadocs for a handy table describing how various Node types behave.

Well, yeah, if your problem description says something like "If environment ID is 1 then do something", then you're going to need an if-statement in your Java code. Most likely, anyway, there might be other solutions depending on the context. But I don't think we know enough about the context to try anything else, so I'd start with that.