Solved

please help on a bug --URGENT

Posted on 2006-06-13
2
208 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 98
What's wrong with this code? 4 34
eclipse console opening separately 2 37
servlet and mdb, jms error 1 57
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 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