Solved

please help on a bug --URGENT

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now