Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DOM : how to retrieve value of element

 
Priyadarshini Anand
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!..
 
Priyadarshini Anand
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Balaji and william for making me correct.Thanks Balaji for links.

Priya
 
Priyadarshini Anand
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balaji my problem is solved with the example u send.Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic