File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading xml from file

 
Adewale Adebusoye
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
sachin poddar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic