Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

please help on a bug --URGENT

Posted on 2006-06-13
2
Medium Priority
?
221 Views
Last Modified: 2010-03-31
I need get data from an xml file

void retrieveData(String xml_file){
                  
Document xmlDoc = null;  
ArrayList list = new ArrayList() ;
            
try
{DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder docbuilder = docbuilderfactory.newDocumentBuilder();
  xmlDoc = docbuilder.parse(xml_file);  
}
catch (Exception e)
{
    System.out.println("Cannot read the XML file: "+e.getMessage());
}
     
try
{      
//Get the root element and all Record elements
Element recordRoot = xmlDoc.getDocumentElement();
NodeList records = recordRoot.getElementsByTagName("ORDER");
  for (int i = 0; i < records.getLength(); i++)
  {
  //For each RECORD, get the RECORD element
  Element record = (Element)records.item(i);
                        
  //Loop through each  for the record
  String s_id = record.getElementsByTagName("ID").item(0).getFirstChild().getNodeValue();
  String name = record.getElementsByTagName("NAME").item(0).getFirstChild().getNodeValue();  
      .................                                                                   
  }//EDN OF FOR
}
catch (Exception e)
{
System.out.println("Error creating class: "+e.getMessage());
return null;
}
                  
}

IF the input xml file looks like

<ORDER>
<ID>2</ID>
<NAME>a</NAME>
</ORDED>

the method above works

if  the input xml file looks like

<ORDER>
<ID>2</ID>
<NAME></NAME>
</ORDED>

when the method runs at

String name = record.getElementsByTagName("NAME").item(0).getFirstChild().getNodeValue();  

it will be caught by catch block, how to handle this case when <NAME></NAME> HAS NOTHING IN BETWEEN?

THANKS
0
Comment
Question by:keepworking
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16897839
1. Check

record.getElementsByTagName("NAME")

length

2. record.getElementsByTagName("NAME").item(0)

hasChildNodes()

and you'll be OK
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16897907
:-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question