在此方法中,讀取XML文件並將其中的某些信息放入列表中。我有其他所有的代碼/其他方法工作,我只是無法返回「列表」變量。創建方法,無法返回其中的變量
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);
}
}
你面臨的'麻煩'究竟是什麼? – Nishan 2011-03-27 16:18:05
你打電話什麼方法,你期望什麼 – smas 2011-03-27 16:19:15