java - 使用 for 循环解析 XML 文件

标签 java mysql xml jdbc jsoup

我一直在开发这个程序,它将 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 ());
        }
    }
}

最佳答案

两个独立的库名称冲突 - 因此当您使用 Element 时,编译器会查看 org.w3c.dom.* 中的 Element 接口(interface);而你确实想使用 Jsoup 中的 Element

删除该行 - 导入 org.w3c.dom.*

添加行 导入 org.jsoup.Jsoup.*; (注意*)

关于java - 使用 for 循环解析 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24085414/

相关文章:

java - 我应该在客户端计算机上使用 JVM 还是 JRE 来运行嵌入式 Java DB?

java - 如何在现有背景图像上添加侧边菜单(导航菜单)

java - Android:java.lang.IllegalStateException:适配器的内容已更改但ListView未收到通知

MySQL COMRESS DECOMPRESS 函数的 Java 模拟

java - 使用hibernate更新mysql中的DateTime类型

java - 将 http url 传递到 SAXParser XML 中

java - 使用 SAXParser 读取 S3 中的 XML 文件

java - JAXB,公共(public)字段映射行为与带有 @XmlElement 注释的私有(private)字段不同

mysql - MySQL 是否会短路 ORDER BY 子句?

sql - 带有 namespace 的 openxml