freelanceprogrammers.org Forum Index » Java
Read element from XML
Joined: 15 Feb 2002
Posts: 3
Read element from XML
How can I read and writethe values from XML file by jsp or java beans file?
Joined: 14 Feb 2002
Posts: 50
Read element from XML
You need an XML parser. You can either go with a
low-level parser such as Xerces or Crimson
(<a href=http://xml.apache.org/ target=new>http://xml.apache.org/</a>) or
something a little bit higher level such as
JDOM (<a href=http://www.jdom.org/ target=new>http://www.jdom.org/</a>) or Dom4J
(<a href=http://www.dom4j.com/ target=new>http://www.dom4j.com/</a>). 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.<br><br>For example, using Dom4J you would find a value like
this:<br><br>SAXReader reader = new SAXReader();<br>Document document =
reader.read(new FileInputStream("file.xml"));<br>Element
rootElement = document.getRootElement();<br><br>That would
give you the root element - then you can walk the XML
tree to find the element you want:<br><br>Element
element = rootElement.getChild("firstChild");<br>String
text = element.getText();<br><br>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
easy.<br><br>Sincerely,<br>Anthony Eden
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







