File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes reading xml from file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "reading xml from file" Watch "reading xml from file" New topic
Author

reading xml from file

Adewale Adebusoye
Ranch Hand

Joined: Sep 28, 2005
Posts: 118
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


"Human beings can alter their lives by altering their state of mind" William James
sachin poddar
Greenhorn

Joined: Feb 10, 2006
Posts: 17
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. [SCJP 1.4, SCWCD 5]
sachin poddar
Greenhorn

Joined: Feb 10, 2006
Posts: 17
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
 
subject: reading xml from file