Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing an xml file from an uri

 
snoofle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all,
i am learning xml and now i want to access a xml file like this and want to access the values.

my question now is:
how can i access the value from the user - userrole like:

can somebody help me please.
tia
andreas spiessl
 
lavanya subramanian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hio andreas,
Welcome to XML.

first the xml code is not well-formed,There are many occurances of '<' and '>' at unwanted places,so correct that.

i'm sending u the code for ur problem

try
{
DOMParser parser = new DOMParser();
parser.parse("your file goes here");
Document doc = parser.getDocument();
NodeList nl1 = doc.getElementsByTagName("userrole");
Node n = null;
for(int i=0;i<nl1.getLength();i++)
{
n = nl1.item(i);
if(n.getNodeType() == Node.ELEMENT_NODE)
{
if(n.hasChildNodes())
{
NodeList nl2 = n.getChildNodes();
String temp[] = new String(nl2.getLength());
for(int k=0;k<nl2.getLength();k++)
{
temp[k]=nl2.item(k).getNodeValue());
}
}
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
This would help.
 
snoofle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi lavanya,
sorry for my late reply.
it worked fine, thank you for the help.
regards andreas spiessl
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic