Accessing child elements

I have built a XML record on Taminothat has elements with an array. I can access the other elements of the object, but when I try and access the children elements I get an array error:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:511)
at java.util.ArrayList.get(ArrayList.java:327)
at com.softwareag.tamino.db.api.examples.greeting.query.main(query.java:150)
Exception in thread “main”


Java code:
StringWriter stringWriter = new StringWriter();
response.getFirstXMLObject().writeTo( stringWriter);
System.out.println( “Retrieved following instance:” + stringWriter );
String data = “Collection”;
// this stest the commit to local
// TLocalTransaction localTransaction = connection.useLocalTransactionMode();
while (test1.hasNext())
{
TXMLObject xmlObject1 = test1.next();
Element e = (Element) xmlObject1.getElement();
List list = e.elements(tagName);
Element elem = (Element) list.get(0);
elem.node(0).getStringValue();
data = (elem.node(0).getStringValue()) ;
System.out.println(“Before update” + data);
TXMLObject xmlobject = test1.next();
System.out.println(“i got xmlobject object”);
Element t = (Element) xmlobject.getElement();
List tlist = t.elements(“taxkey”);
Element taxkey_elem = (Element) tlist.get(0);
String taxkey = taxkey_elem.node(0).getStringValue();
Element oe = (Element) xmlobject.getElement();
list = oe.elements(“report_line”);
Element levy_year_elem = (Element) list.get(0);
String levy_year = levy_year_elem.node(0).getStringValue();
System.out.println(taxkey);
System.out.println(levy_year);
System.out.print(" (");
System.out.print(“ino:id=”" + xmlobject.getId() + “” “);
System.out.print(“collection=”” + xmlobject.getCollection() + “” “);
System.out.print(“doctype=”” + xmlobject.getDoctype() + “” “);
System.out.println(”)");
// response = xmlObjectAccessor.update(xmlObject1);
// End of Update
//System.out.println(" Update completed");

}

The XML is :
<?xml version="1.0" encoding="utf-8"?>

3220785000

<check_digit>3</check_digit>

null

<levy_year>1983</levy_year>



<report_line num=“1”> 322-0785-3 LEVYR 1983 TXYR 83 2452 N 3RD ST </report_line>

<report_line num=“2”> STAT A= 6 I= E= 1U REM TAX RATE 01 PLAT CURR CLS CURR</report_line>

<report_line num=“3”> OWNER: VERNON TOWNSEND, ROTHCHILD TOWNSEND - CHARLEY TOWNSEND 2338 N 2ND</report_line>

<report_line num=“4”> LAND REV LAND REV CDE TOT SPEC </report_line>

<report_line num=“5”> IMPRV REV IMPRV REV TOT SPEC </report_line>

<report_line num=“6”> TOT CITY DUE 1569.63 TOT CITY PD 1569.63 TOT CREDIT </report_line>

<report_line num=“7”> 19880127 19880127 999 491.64- </report_line>

<report_line num=“8”> 19891025 19891025 995 </report_line>

<report_line num=“9”> </report_line>

<report_line num=“10”> 19891025 19891025 995 </report_line>

<report_line num=“11”> 19910301 19910301 995 </report_line>

<report_line num=“12”> </report_line>

<report_line num=“13”> 19920925 19920925 995 </report_line>

<report_line num=“14”> </report_line>

<report_line num=“15”> 19920925 19920925 995 </report_line>

<report_line num=“16”> </report_line>

<report_line num=“17”> 19930622 19930622 995 </report_line>

<report_line num=“18”> </report_line>

<report_line num=“19”> 19930622 19930622 995 </report_line>

<report_line num=“20”> 19930622 19930622 995 </report_line>

<report_line num=“21”> 19931013 19931013 995 </report_line>

<report_line num=“22”> </report_line>

<report_line num=“23”> 19950608 19950608 </report_line>

<report_line num=“24”> 7C </report_line>

<report_line num=“25”> 19960913 19960913 994 </report_line>

<report_line num=“26”> 4 </report_line>

<report_line num=“27”> 19960913 19960913 994 </report_line>

<report_line num=“28”> 4 </report_line>

<report_line num=“29”> 19961111 19961111 994 </report_line>

<report_line num=“30”> 8W </report_line>

<report_line num=“31”> 19961111 19961111 994 </report_line>

<report_line num=“32”> 8W </report_line>

<report_line num=“33”> 19961209 19961209 </report_line>

<report_line num=“34”> 8W </report_line>

<report_line num=“35”> 19961209 19961209 994 </report_line>

<report_line num=“36”> 8W </report_line>

<report_line num=“37”> 19961216 19961216 </report_line>

<report_line num=“38”> 8W </report_line>

<report_line num=“39”> 19961216 19961216 </report_line>

<report_line num=“40”> 8W </report_line>

<report_line num=“41”> 19971211 19971211 </report_line>

<report_line num=“42”> 8W </report_line>

<report_line num=“43”> 19971211 19971211 </report_line>

<report_line num=“44”> 8W </report_line>

<report_line num=“45”> 19980911 19980911 994 </report_line>

<report_line num=“46”> 7W </report_line>

<report_line num=“47”> 19980911 19980911 994 </report_line>

<report_line num=“48”> 7W </report_line>

<report_line num=“49”> 19981208 19981208 </report_line>

<report_line num=“50”> 7W </report_line>

<report_line num=“51”> 19990721 19990721 994 </report_line>

<report_line num=“52”> 8W </report_line>

<report_line num=“53”> 19990721 19990721 994 </report_line>

<report_line num=“54”> 8W </report_line>

<report_line num=“55”> 19991213 19991213 </report_line>

<report_line num=“56”> 8W </report_line>

<report_line num=“57”> 19991213 19991213 </report_line>

<report_line num=“58”> 8W </report_line>

<report_line num=“59”> 20000222 20000222 </report_line>

<report_line num=“60”> 8W </report_line>

<report_line num=“61”> 20000222 20000222 </report_line>

<report_line num=“62”> 8W </report_line>

<report_line num=“63”> 20000920 20000920 994 </report_line>

<report_line num=“64”>0 7W </report_line>

<report_line num=“65”> 20000920 20000920 994 </report_line>

<report_line num=“66”> 7W </report_line>

<report_line num=“67”> 20001207 20001207 </report_line>

<report_line num=“68”> 7W </report_line>

<report_line num=“69”> 20001207 20001207 </report_line>

<report_line num=“70”> 7W </report_line>

<report_line num=“71”> 20011114 20011114 </report_line>

<report_line num=“72”> 7C </report_line>

<report_line num=“73”> 20011211 20011211 </report_line>

<report_line num=“74”> 7C </report_line>

<report_line num=“75”> 20011211 20011211 </report_line>

<report_line num=“76”> 7C </report_line>

<report_line num=“77”> 20011212 20011212 </report_line>

<report_line num=“78”> 7C </report_line>

<report_line num=“79”> 20011212 20011212 </report_line>

<report_line num=“80”> 7C </report_line>

<report_line num=“81”> 20020313 20020313 3 56.18 112.92 </report_line>

<report_line num=“82”> 7C </report_line>

<report_line num=“83”> 20020611 20020611 2 124.28 249.80 </report_line>

<report_line num=“84”> 7C </report_line>

<report_line num=“85”> 20020807 20020807 6 77.71 156.20 </report_line>

<report_line num=“86”> 7C </report_line>

<report_line num=“87”> 20020910 20020910 2 77.71 156.20 </report_line>

<report_line num=“88”> 7C </report_line>

<report_line num=“89”> 20021008 20021008 6 77.46 155.69 </report_line>

<report_line num=“90”> 7C </report_line>

<report_line num=“91”> 20021107 20021107 1 150.04 78.30 301.59 157.38 </report_line>

<report_line num=“92”> 7C </report_line>

<report_line num=“93”> 20021211 20021211 2 101.33 203.67 </report_line>

<report_line num=“94”> 7C </report_line>

<report_line num=“95”> 20030107 20030107 6 101.33 203.67 </report_line>

<report_line num=“96”> 7C </report_line>

<report_line num=“97”> 20030122 20030122 6 101.33 203.67 </report_line>

<report_line num=“98”> 7C </report_line>

<report_line num=“99”> 20030124 20030124 994 </report_line>

<report_line num=“100”> 7T </report_line>

<report_line num=“101”> 20030124 20030124 994 </report_line>

<report_line num=“102”> 7T </report_line>

<report_line num=“103”> 20030124 20030124 994 </report_line>

<report_line num=“104”> 7T </report_line>

<report_line num=“105”> 20030128 20030128 994 </report_line>

<report_line num=“106”> 1U </report_line>

<report_line num=“107”> 20030131 20030131 4 1115.60 </report_line>

<report_line num=“108”> 1U </report_line>



What Document Object Model are you using; JDOM or DOM? I don’t recognise this combination of Classes and Methods in your code:

Element e = (Element) xmlObject1.getElement();
List list = e.elements(tagName);


Could you please post all of the code, the xml instance and the corresponding schema as a zip file, then I can try to help more.
Thanks.

Included is the results from console, the schema ad the java source code
Query_results.txt (24.4 KB)

You’re using DOM4J. I’m not sure what some of your code is for (you’re doing another query when you already have the report line data available) but maybe thats just because I don’t understand your data? Anyway, to iterate through your report lines I changed your code like this and it seems to work:

while (test1.hasNext()) {
				TXMLObject xmlObject1 = test1.next();
				Element e = (Element) xmlObject1.getElement();
				List list = e.elements(tagName);
				Element elem = (Element) list.get(0);
				elem.node(0).getStringValue();
				data = (elem.node(0).getStringValue());
				System.out.println("Taxkey" + data);
				elem.setText(data);
		// don't use next() without checking that its really available....		
				TXMLObject xmlobject = xmlObject1;
				System.out.println("i got xmlobject object");
				Element oe = (Element) xmlobject.getElement();
				
				List listoe = oe.elements("Collection");
				int lengthList = listoe.size();
				Element levy_year_elem2 = (Element) listoe.get(0);
				
				List listrl = levy_year_elem2.elements("report_line");
				int lengthrl = listrl.size();
				for (int j=0;j<lengthrl;j++)
					{
 						Element reportLine = (Element) listrl.get(j);
 						String report_line = reportLine.getStringValue();
						System.out.println("report_line" + report_line + "J" + j);
				        }
				System.out.print(" (");
				System.out.print("ino:id=\"" + xmlobject.getId() + "\" ");
				System.out.print(
					"collection=\"" + xmlobject.getCollection() + "\" ");
				System.out.print("doctype=\"" + xmlobject.getDoctype() + "\" ");
				System.out.println(")");
		
			}