Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Parse error

Status
Not open for further replies.

sheed

Programmer
Jun 14, 2005
38
US
Hi,

I have a following code and need some help. When I try to run it it gives me the following error:

Code:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.RangeCheck(ArrayList.java:507)
	at java.util.ArrayList.get(ArrayList.java:324)
	at LoadWithDOM.printElements(LoadWithDOM.java:37)
	at LoadWithDOM.main(LoadWithDOM.java:25)

Here is the code I am trying to run:
Code:
import java.io.FileNotFoundException;
import java.io.IOException;

import oracle.xml.parser.v2.DOMParser;

import java.io.*;
import java.util.*;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.DOMReader;
import org.dom4j.Element;

public class LoadWithDOM
{
	public static void main(String[] args)
  {
	  try
	  {
			LoadWithDOM lWithDOM = new LoadWithDOM();      
      DOMParser parser = new DOMParser();
      parser.parse(new FileInputStream("C://Samples//Parse//test.xml"));
      org.w3c.dom.Element element = parser.getDocument().getDocumentElement();      
			lWithDOM.printElements(element);
	  }
    catch(Exception e)
	  {
      e.printStackTrace();
	  }
	}
	
	public void printElements(org.w3c.dom.Element request) throws DocumentException
	{			
      DOMReader reader = new DOMReader();
      Document doc = reader.read(request.getOwnerDocument());      
      Element n = ((Element)((Element)doc.getRootElement().elements().get(0)).elements().get(0));
     
      Iterator elementIterator = n.elementIterator("innersecond");   
      while(elementIterator.hasNext())
      {
        Element element = (Element)elementIterator.next();
        System.out.println("Element name " + element.getName());  
      }      
		}
}

I can't really figure it out so need some help in debugging it and why am I getting the error as it throughs an error on
Element n = ((Element)((Element)doc.getRootElement().elements().get(0)).elements().get(0));

Here is the sample xml file I am using:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<outer>
	<firstrow>firstrow</firstrow>
	<secondrow>secondrow</secondrow>
	<thirdrow>thirdrow</thirdrow>
	<fourthrow>fourthrow</fourthrow>
	<lastr>lastr</lastr>
	<innerfirst>
		<birthdayMonth>January</birthdayMonth>
		<birthdayDay>21</birthdayDay>
		<birthdayYear>1983</birthdayYear>
		<secondlevel>
			<first>first</first>
			<second>second</second>
			<thirdlevel>
				<fourth>fourth</fourth>
			</thirdlevel>
			<thirdlevels>
				<fourths>fourths</fourths>
			</thirdlevels>
		</secondlevel>
		<test>test</test>
		<secondlevelss>
			<firsts>first</firsts>
			<seconds>second</seconds>
			<thirdlevels>
				<fourths>fourth</fourths>
			</thirdlevels>
			<thirdlevelss>
				<fourthss>fourths</fourthss>
				<fifth>
					<fifthf>fifthf</fifthf>
					<sixth>
						<sixthf>sixth</sixthf>
					</sixth>
				</fifth>
			</thirdlevelss>
		</secondlevelss>
	</innerfirst>
	<innersecond>
		<value1>value1</value1>
		<value2>value2</value2>
		<value3>val3</value3>
	</innersecond>
</outer>

Now if I remove the following from the test file I don't get any error but the while never gets executed either:

Code:
<firstrow>firstrow</firstrow>
	<secondrow>secondrow</secondrow>
	<thirdrow>thirdrow</thirdrow>
	<fourthrow>fourthrow</fourthrow>
	<lastr>lastr</lastr>

Can someone help > Thanks
 
I think it is because this line :

Code:
Element n = ((Element)((Element)doc.getRootElement().elements().get(0)).elements().get(0));

is asking for :

Get the document root.
Get the first child from that (<firstrow>).
Get the first child from that tag ... but that tag has no children.

You want something a bit more like :

Code:
NodeList list = (((Element)doc.getRootElement().elements();
if (list.getLength() > 0) {
   // and so on
} else {
   // and so on
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top