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 DOM : how to retrieve value of element 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 "DOM : how to retrieve value of element" Watch "DOM : how to retrieve value of element" New topic
Author

DOM : how to retrieve value of element

Priyadarshini Anand
Greenhorn

Joined: Jun 02, 2004
Posts: 22
Hi ,

if i pass the following xml file as input , i need to retrieve the value of passCount and failCount in my java program. I am new to the concepts of DOM and SAX. please guide me how to proceed to solve the problem.


<?xml version='1.0'?>
<?xml-stylesheet type='text/xsl' href='testReport.xsl'?>
<TestReport xmlns="http://ora/tia/fw/b2b/xmlmodel/testreport">
<TestSummary>
<product>B2B</product>
<build>B2B_SOLARIS_BETA</build>
<testName>rnRequirements.xml</testName>
<passCount>3</passCount>
<failCount>1</failCount>
</TestSummary>
</TestReport>


--Priya
Priyadarshini Anand
Greenhorn

Joined: Jun 02, 2004
Posts: 22
I am trying with following code, where i am wrong :


Document doc = parseXmlFile("testReport.xml", false);
Element passelement = (Element)doc.getElementsByTagName("passCount");
Element failelement = (Element)doc.getElementsByTagName("failCount");
Text passtext = (Text)passelement.getFirstChild();
String passstring = passtext.getData();
Text failtext = (Text)failelement.getFirstChild();
String failstring = failtext.getData();


pl help me.

--Priya
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
You must be getting a lot of compiler errors. Get a copy of the XML API and chase them down one by one. For example:


getElementsByTagName() is declared to return a NodeList, not an Element
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
The tutorial Understanding DOM will also help you to understand the basics of DOM & JAVA...
Using getElementsByTagName is one wise method, try to use them after correcting the errors indicated by William...
Also see this example http://faq.javaranch.com/view?GetElementValueByNameUsingDom for sure!..


Spritle Software Blogs
Priyadarshini Anand
Greenhorn

Joined: Jun 02, 2004
Posts: 22
Thanks Balaji and william for making me correct.Thanks Balaji for links.

Priya
Priyadarshini Anand
Greenhorn

Joined: Jun 02, 2004
Posts: 22
Balaji my problem is solved with the example u send.Thanks again.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: DOM : how to retrieve value of element