[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

please help on a bug --URGENT

Posted on 2006-06-13
2
Medium Priority
?
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

650 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