We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › q: re XML and dynamic ArrayLists
Page Index Toggle Pages: 1
q: re XML and dynamic ArrayLists (Read 993 times)
q: re XML and dynamic ArrayLists
Nov 4th, 2006, 7:20pm
 
Hi,

I'm working on a program to dynamically parse data within XML nodes and store that info inside ArrayLists. I'm using the pro-XML class. I can do what I need to do (put the info from every node within a separate ArrayList) except my code looks AWFUL. I don't know if there are ways to dynamically generate Arrays / ArrayLists. Maybe someone can offer a suggestion?

Here is a sample of the XML file:

Code:


<record>
<company_name>1-800-ATTORNEY Inc</company_name>
<ticker_symbol>ATTY</ticker_symbol>
<industry_name>Publishing</industry_name>
<sic>2,700 </sic>
<exchange>NDQ</exchange>
<country>US</country>
<stock_price>1 </stock_price>
<%_chg_in_last_year>0 </%_chg_in_last_year>
<trading_volume>1,438 </trading_volume>
<#_of_shares_outstanding>0 </#_of_shares_outstanding>
<market_cap>0 </market_cap>
<total_debt>0 </total_debt>
<firm_value>0 </firm_value>
<company_name>1-800-ATTORNEY Inc</company_name>
<ticker_symbol>ATTY</ticker_symbol>
<industry_name>Publishing</industry_name>
<sic>2,700 </sic>
<exchange>NDQ</exchange>
<country>US</country>
</record>



And here is the code:

Code:

/*
Test xml loader
*/

import proxml.*;

XMLInOut xmlIO;

//Arrays to store first run-through

XMLElement[] child;
XMLElement[] child2;
XMLElement[] child3;
XMLElement[] child4;
XMLElement[] child5;
XMLElement[] child6;
XMLElement[] child7;
XMLElement[] child8;
XMLElement[] child9;

//ArrayLists to convert Arrays to ArrayList - each one holds EVERY <x></x> node within the XML file

ArrayList myList = new ArrayList();
ArrayList myList2 = new ArrayList();
ArrayList myList3 = new ArrayList();
ArrayList myList4 = new ArrayList();
ArrayList myList5 = new ArrayList();
ArrayList myList6 = new ArrayList();
ArrayList myList7 = new ArrayList();
ArrayList myList8 = new ArrayList();
ArrayList myList9 = new ArrayList();

// Takes each(x) item from the ArrayLists and stores it within another ArrayList holding all of the information for that node

ArrayList complete1 = new ArrayList();
ArrayList complete2 = new ArrayList();
ArrayList complete3 = new ArrayList();
ArrayList complete4 = new ArrayList();
ArrayList complete5 = new ArrayList();
ArrayList complete6 = new ArrayList();
ArrayList complete7 = new ArrayList();
ArrayList complete8 = new ArrayList();
ArrayList complete9 = new ArrayList();


void setup(){
size(1,1);

xmlIO = new XMLInOut(this);
xmlIO.loadElement("xl_xml_new.xml");

}

// This returns the FIRST name of EVERY record in the XML file:

void xmlEvent(XMLElement element){
XMLElement[] children = element.getChildren();

println(children.length);

for(int i = 0; i < children.length;i++){
child = new XMLElement[9];
child2 = new XMLElement[9];
child3 = new XMLElement[9];
child4 = new XMLElement[9];
child5 = new XMLElement[9];
child6 = new XMLElement[9];
child7 = new XMLElement[9];
child8 = new XMLElement[9];
child9 = new XMLElement[9];
child = children[i].firstChild().getChildren();
child2 = children[i].firstChild().nextSibling().getChildren();
child3 = children[i].firstChild().nextSibling().nextSibling().getChildren();
child4 = children[i].firstChild().nextSibling().nextSibling().nextSibling().getChildren();
child5 = children[i].firstChild().nextSibling().nextSibling().nextSibling().nextSibling().getChildren();
child6 = children[i].firstChild().nextSibling().nextSibling().nextSibling().nextSibling().nextSibling().getChildren();
child7 = children[i].firstChild().nextSibling().nextSibling().nextSibling().nextSibling().nextSibling().nextSibling().getChildren();
child8 = children[i].firstChild().nextSibling().nextSibling().nextSibling().nextSibling().nextSibling().nextSibling().nextSibling().getChildren();
child9 = children[i].firstChild().nextSibling().nextSibling().nextSibling().nextSibling().nextSibling().nextSibling().nextSibling().nextSibling().getChildren();
child.toString();
child2.toString();
child3.toString();
child4.toString();
child5.toString();
child6.toString();
child7.toString();
child8.toString();
child9.toString();

myList.add(child);
myList2.add(child2);
myList3.add(child3);
myList4.add(child4);
myList5.add(child5);
myList6.add(child6);
myList7.add(child7);
myList8.add(child8);
myList9.add(child9);
//println(child);
//println(child2);
}

println(myList6.size());
println(myList7.get(0));


for (int j = 0; j < 1;j++) {
complete1.add(myList.get(j));
complete1.add(myList2.get(j));
complete1.add(myList3.get(j));
complete1.add(myList4.get(j));
complete1.add(myList5.get(j));
complete1.add(myList6.get(j));
complete1.add(myList7.get(j));
complete1.add(myList8.get(j));
complete1.add(myList9.get(j));
}
println(complete1.get(8));
println(complete1.size());


}



void draw(){
}



I'm essentially trying to iterate through an XML file and store each <record> within an ArrayList
Page Index Toggle Pages: 1