1

I need to check if the environment id matches I think to the db id within an xml file so I know which db elements to update. I have the code:

//get tag           
    Node environment = doc.getElementsByTagName("environment").item(0);

    //check environment id 
     if(environment.getEnvironmentID().equals())
     {

     }

The code I have after is for the nodelist and the elements within that are to be updated. I just don't know how to finish off that check to get the right one.

xml file:

 <defaultenv id = "default">  
<Environment id = "name">  
<Db id = "db1">  
<Website> BlueHouse.co.uk</Website>  
</Db>  
</Environment>  
</Defaultenv> 

Can anyone point me in the right direction?

2 Answers2

1

Since you're using org.w3c.dom.Node, use one the following snippets to get the environment id.

Option 1

//get tag           
// ** I use "Environment" with upper E because XML is case-sensitive
Node environment = doc.getElementsByTagName("Environment").item(0);

//check environment id 
if(environment.getAttributes().getNamedItem("id").getNodeValue().equals("..."))
{

}

Option 2

If you don't mind cast in your code, you can do this too:

//get tag           
Node environment = doc.getElementsByTagName("Environment").item(0);

//check environment id 
// ** I cast the Node instance (ie environment) into an Element instance
if ( ((Element)environment).getAttribute("id").equals("...") )
{

}
Stephan
  • 41,764
  • 65
  • 238
  • 329
0

I dint understood your question properly. felt like you want to compare the id attribute of environment tag and DB tag with some default value or among themselves. As per the XML sample given, following code will give you the id value of both tags.Hope it can be used for comparison.

Node environment= doc.getElementsByTagName("Environment").item(0);

     NamedNodeMap nodeMap = environment.getAttributes();

     Node idAttr = nodeMap.getNamedItem("id");

     String envAttrValue = idAttr.getTextContent();

     Node dbNode = node.getChildNodes().item(0);

     NamedNodeMap dbNodeMap = dbNode.getAttributes();

     Node dbIdAttr = dbNodeMap.getNamedItem("id");

     String dbAttrValue = dbIdAttr.getTextContent();
Renjith
  • 1,122
  • 1
  • 19
  • 45
  • I need to check what the environment id is as certain environments can contain the db I want to update - dont know if that makes it clearer? Also thankyou for taking your time for that piece of code – user3373261 Sep 12 '14 at 09:28
  • envAttrValue, will give you the id value for environment node. dbAttrValue, will give you the id value for DB node. – Renjith Sep 12 '14 at 09:31
  • if I did that : //get tag directly Node enviro = doc.getElementsByTagName("environment").item(0); //check for database type to see which the updated/created elements are to go into if(environment.getEnvironmentID().equals(enviro)) { } Would that work? I cant use the idAttr and dbIdAttr as they dont exist – user3373261 Sep 12 '14 at 10:34
  • Node enviro = doc.getElementsByTagName("environment").item(0); will give you ''. enviro.getAttributes() will give you a map of the attributes in 'Environment' tag. i.e. id=name. map contains an entry with 'id' as key and a node representing 'id=name' as value. if you take the text content of this node 'idAttr.getTextContent()', you will get the value 'name'. now tell me what you need to do with this value – Renjith Sep 12 '14 at 10:52
  • I need to compare it against a dbpool id to see if the id matches the database im after :). The database must exist in the environment for the environment id to match the dbpool id – user3373261 Sep 12 '14 at 11:27