我正在尝试将xml文件名传递给函数(getAllXmlData
)并提取其数据并将其存储在多维数组中,并通过xmlArray
将xml数据返回到第一个类。我有以下代码:
public class Utils {
static String[][] getAllXmlData(String strIn1) {
File file = new File(strIn1);
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
NodeList nodes = doc.getElementsByTagName("person");
System.out.println("Total of elements : " + nodes.getLength());
String xmlArray[][] = new String[nodes.getLength()][12];
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList recordID = element.getElementsByTagName("RecordID");
Element line = (Element) recordID.item(0);
xmlArray[i][1] = line.getFirstChild().getTextContent();
xmlArray[i][2] = line.getAttribute("id");
NodeList lastName = element.getElementsByTagName("LastName");
line = (Element) lastName.item(0);
xmlArray[i][3] = line.getFirstChild().getTextContent();
NodeList firstName = element.getElementsByTagName("FirstName");
line = (Element) firstName.item(0);
xmlArray[i][4] = line.getFirstChild().getTextContent();
NodeList title = element.getElementsByTagName("Title");
line = (Element) title.item(0);
xmlArray[i][5] = line.getFirstChild().getTextContent();
NodeList hobby = element.getElementsByTagName("Hobby");
line = (Element) hobby.item(0);
xmlArray[i][6] = line.getFirstChild().getTextContent();
NodeList phone1 = element.getElementsByTagName("CellPhone");
line = (Element) phone1.item(0);
xmlArray[i][7] = line.getFirstChild().getTextContent();
}
} catch (Exception e) {
e.printStackTrace();
}
return xmlArray;
}
}
但收到以下错误:
Utils.java:54: error: cannot find symbol
return xmlArray;
symbol: vailable xmlArray
location: class Utils
我赞美任何帮助。此外,如果有更多高效和动态的方式将xml数据存储在数组中而不必通过elemnt执行元素。 THX
答案 0 :(得分:0)
将xmlArray移出try / catch:
public class Utils {
static String[][] getAllXmlData(String strIn1) {
File file = new File(strIn1);
String xmlArray[][] = null;
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
NodeList nodes = doc.getElementsByTagName("person");
System.out.println("Total of elements : " + nodes.getLength());
xmlArray = new String[nodes.getLength()][12];
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList recordID = element.getElementsByTagName("RecordID");
Element line = (Element) recordID.item(0);
xmlArray[i][1]=line.getFirstChild().getTextContent();
xmlArray[i][2]=line.getAttribute("id");
NodeList lastName = element.getElementsByTagName("LastName");
line = (Element) lastName.item(0);
xmlArray[i][3]=line.getFirstChild().getTextContent();
NodeList firstName = element.getElementsByTagName("FirstName");
line = (Element) firstName.item(0);
xmlArray[i][4]=line.getFirstChild().getTextContent();
NodeList title = element.getElementsByTagName("Title");
line = (Element) title.item(0);
xmlArray[i][5]=line.getFirstChild().getTextContent();
NodeList hobby = element.getElementsByTagName("Hobby");
line = (Element) hobby.item(0);
xmlArray[i][6]=line.getFirstChild().getTextContent();
NodeList phone1 = element.getElementsByTagName("CellPhone");
line = (Element) phone1.item(0);
xmlArray[i][7]=line.getFirstChild().getTextContent();
}
}
catch (Exception e) {
e.printStackTrace();
}
return xmlArray;
}
}