Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Read element from XML

  Asked By: Sunil    Date: Jul 28    Category: Java    Views: 1247

How can I read and writethe values from XML file by jsp or java beans file?



1 Answer Found

Answer #1    Answered By: Joyce Edwards     Answered On: Jul 28

You need an xml  parser. You can either go with a
low-level parser such as Xerces or Crimson
(http://xml.apache.org/) or
something a little bit higher level such as
JDOM (http://www.jdom.org/) or Dom4J
(http://www.dom4j.com/). The nice
thing about JDOM and Dom4J is that they
create Java object models which are easier to work with
than the DOM or SAX models that lower-level parsers
produce.For example, using Dom4J you would find a value like
this:SAXReader reader = new SAXReader();Document document =
reader.read(new FileInputStream("file.xml"));Element
rootElement = document.getRootElement();That would
give you the root element  - then you can walk the XML
tree to find the element you want:Element
element = rootElement.getChild("firstChild");String
text = element.getText();There are also other
ways such as XPath, but you will need to look at that
on your own. Take a look at Dom4J or JDOM - trust me
when I say that it makes dealing with XML documents

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