我一直在研究这个将XML文件插入MYSQL数据库的程序。我通过插入包来了解整个.jar的想法。我遇到了parse(),select()和children()的问题。有人可以告诉我如何解决这个问题吗?这是我的堆栈跟踪和我的程序如下:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method select(String) is undefined for the type Document
The method children() is undefined for the type Element
The method children() is undefined for the type Element
The method children() is undefined for the type Element
The method children() is undefined for the type Element
at jdbc.parseXML.main(parseXML.java:28)
import java.io.*;
import java.sql.*;
import org.jsoup.Jsoup;
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class parseXML{
public static void main(String xml) {
try{
BufferedReader br = new BufferedReader(new FileReader(new File("C:\\staff.xml")));
String line;
StringBuilder sb = new StringBuilder();
while((line=br.readLine())!= null){
sb.append(line.trim());
}
Document doc = Jsoup.parse(line);
StringBuilder queryBuilder;
StringBuilder columnNames;
StringBuilder values;
for (Element row : doc.select("row")) {
// Start the query
queryBuilder = new StringBuilder("insert into customer(");
columnNames = new StringBuilder();
values = new StringBuilder();
for (int x = 0; x < row.children().size(); x++) {
// Append the column name and it's value
columnNames.append(row.children().get(x).tagName());
values.append(row.children().get(x).text());
if (x != row.children().size() - 1) {
// If this is not the last item, append a comma
columnNames.append(",");
values.append(",");
}
else {
// Otherwise, add the closing paranthesis
columnNames.append(")");
values.append(")");
}
}
// Add the column names and values to the query
queryBuilder.append(columnNames);
queryBuilder.append(" values(");
queryBuilder.append(values);
// Print the query
System.out.println(queryBuilder);
}
}catch (Exception err) {
System.out.println(" " + err.getMessage ());
}
}
}
答案 0 :(得分:1)
两个独立的库名称冲突 - 因此当您使用Element时,编译器正在查看org.w3c.dom中的Element接口。*;而你真的想要使用来自Jsoup的元素
删除该行 - import org.w3c.dom。*
添加该行 import org.jsoup.Jsoup。 * ; (注意*)