• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading xml from file

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a jsp page from which im meant to submit an id to a servlet, and then the servlet reads an xml file, and outputs the correct line to the jsp.
the xml file looks like



so if i submit an id of 1 from the jsp, then it should send it to the servlet which reads the file and outputs "hello people".
My problem now is how to read the xml file from the servlet,since I havent done anytype of xml stuff like this before.
Thanks
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Adewale ,
try this-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(fileStream);
String id = document.getElementsByTagName("data").item(0).getAttributes().getNamedItem("id").getNodeValue();

where fileStream is stream to your input file.
One more thing i would like to say is your id attribute value must be in single or double quotes! i.e. like
<data id='1'>

let me know is this is suffice for you!
 
sachin poddar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
by the way i liked your quote-
"Human beings can alter their lives by altering their state of mind" William James

..but sometimes situations do alter our state of mind!
 
reply
    Bookmark Topic Watch Topic
  • New Topic