Solved

please help on a bug --URGENT

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
expectj telnet failing 5 45
MySqlDump not dumping triggers 1 43
servlet example 17 51
How to convert from xls to xlsx using java 7 55
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:

839 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