In this method, an XML file is read and certain information about it is put into a list. I have everything else working for all of the code/other methods, I am just having trouble returning the “list” variable.
package ca3;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class xmlReader{
public String fileName;
public xmlReader (String fileLoca){
fileName=fileLoca;
}
public List list;
public List returnLists(String fileN)
{
mp3Lister woww = null;
String fName;
String lName;
mp3Lister qwewq;
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File(fileN));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("person");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
List list = new LinkedList(); // Doubly-linked list
for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
Element firstNameElement = (Element)firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
// System.out.println("First Name : " +
// ((Node)textFNList.item(0)).getNodeValue().trim());
//
//-------
NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
Element lastNameElement = (Element)lastNameList.item(0);
NodeList textLNList = lastNameElement.getChildNodes();
// System.out.println("Last Name : " +
// ((Node)textLNList.item(0)).getNodeValue().trim());
lName = textLNList.item(0).getNodeValue().trim().toString();
fName = textFNList.item(0).getNodeValue().trim().toString();
mp3Lister[] wowow = null;
woww = new mp3Lister(fName,lName);
list.add(woww);
//----
//------
}//end of if clause
// qwewq = (mp3Lister) list.get(0);
// System.out.println(qwewq.getTitle() + " wakakakakaka");
}//end of for loop with s var
//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
return list;
//System.exit (0);
}
}
I assume that you meant to return the
listlocal variable that you declare in thereturnListsfunction. What you’ll be getting back is actually the class variable, also calledlist. This is because the locallistvariable has gone out of context by the time the code reaches your return statement (the locallistvariable is within the context of thetryblock you have declared). If you want to return the value from the locallistvariable change the first couple of lines ofreturnListsto this:and then remove the declaration of the
listvariable inside the try block.