Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Read XML File using DOM

  Asked By: Kuhaylah    Date: Mar 26    Category: Java    Views: 1222
  

I want to read all my servers from this XML file. Here is the
sample XML file. I would like to read the nodenames of all the
servers(like Server1, Server2,Server3) and populate in a combobox.
Once I select the Server in the combo box then I want to populate
the username and password in a text box. Please let me know how to
proceed.

Here is the code I have so far...

DocumentBuilder builder = DocumentBuilderFactory.newInstance
().newDocumentBuilder();
Document doc = builder.parse(file);

// Find the tags of interest
NodeList listofServers = doc.getElementsByTagName
("Servers");
int totalServers=listofServers.getLength();
for(int i = 0; i < totalServers; i++) {
Node serverNode=listofServers.item(i);
if(serverNode.getNodeType()==Node.ELEMENT_NODE)

{
Element serverElement = (Element)instanceNode;
System.out.println(serverElement.getNodeName());

}

<?xml version="1.0" encoding="UTF-8"?>

<Domain>

<Servers>

<Server1>


<User>testuser</User>

<Passwd>xyz</Passwd>


</Server1>

<Server2>


<User>testuser</User>

<Passwd>xyz</Passwd>


</Server2>

<Server3>


<User>testuser</User>

<Passwd>xyz</Passwd>


</Server3>

</Servers>



</Domain>

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Read XML File using DOM Or get search suggestion and latest updates.




Tagged: