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: 12265
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
posted Jul 20, 2004 09:47:00
0
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.
I agree. Here's the link: jrebel
subject: DOM : how to retrieve value of element