Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用DOM解析器读取XML文件并将输出存储到MySQL中 导入java.io.File; 导入javax.xml.parsers.DocumentBuilder; 导入javax.xml.parsers.DocumentBuilderFactory; 导入org.w3c.dom.Document; 导入org.w3c.dom.Element; 导入org.w3c.dom.Node; 导入org.w3c.dom.NodeList; 公共类XMLParser{ public void getAllUserNames(字符串文件名){ 试一试{ DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dbf.newDocumentBuilder(); 文件=新文件(文件名); if(file.exists()){ 文档doc=db.parse(文件); Element docEle=doc.getDocumentElement(); //打印文档的根元素 System.out.println(“文档的根元素:” +docEle.getNodeName()); NodeList studentList=docEle.getElementsByTagName(“学生”); //打印文档中的学生元素总数 系统输出 .println(“学生总数:+studentList.getLength()); if(studentList!=null&&studentList.getLength()>0){ 对于(int i=0;i_Java - Fatal编程技术网

使用DOM解析器读取XML文件并将输出存储到MySQL中 导入java.io.File; 导入javax.xml.parsers.DocumentBuilder; 导入javax.xml.parsers.DocumentBuilderFactory; 导入org.w3c.dom.Document; 导入org.w3c.dom.Element; 导入org.w3c.dom.Node; 导入org.w3c.dom.NodeList; 公共类XMLParser{ public void getAllUserNames(字符串文件名){ 试一试{ DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dbf.newDocumentBuilder(); 文件=新文件(文件名); if(file.exists()){ 文档doc=db.parse(文件); Element docEle=doc.getDocumentElement(); //打印文档的根元素 System.out.println(“文档的根元素:” +docEle.getNodeName()); NodeList studentList=docEle.getElementsByTagName(“学生”); //打印文档中的学生元素总数 系统输出 .println(“学生总数:+studentList.getLength()); if(studentList!=null&&studentList.getLength()>0){ 对于(int i=0;i

使用DOM解析器读取XML文件并将输出存储到MySQL中 导入java.io.File; 导入javax.xml.parsers.DocumentBuilder; 导入javax.xml.parsers.DocumentBuilderFactory; 导入org.w3c.dom.Document; 导入org.w3c.dom.Element; 导入org.w3c.dom.Node; 导入org.w3c.dom.NodeList; 公共类XMLParser{ public void getAllUserNames(字符串文件名){ 试一试{ DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dbf.newDocumentBuilder(); 文件=新文件(文件名); if(file.exists()){ 文档doc=db.parse(文件); Element docEle=doc.getDocumentElement(); //打印文档的根元素 System.out.println(“文档的根元素:” +docEle.getNodeName()); NodeList studentList=docEle.getElementsByTagName(“学生”); //打印文档中的学生元素总数 系统输出 .println(“学生总数:+studentList.getLength()); if(studentList!=null&&studentList.getLength()>0){ 对于(int i=0;i,java,Java,使用此代码,我得到的输出为: 输出: 文档的根元素:学生 学生总数:4人 姓名:约翰 等级:B 年龄:12 姓名:玛丽 成绩:A 年龄:11 姓名:西蒙 成绩:A 年龄:18 姓名:Rasika 成绩:A+ 年龄:23 我想将上述输出存储到MySQL中。有人能帮忙吗 确保您有用于MySQL的JDBC驱动程序jar,无论是从依赖项管理器还是使用依赖项管理器 从java连接 import java.io.File; import javax.xml.parsers.DocumentBuilder;

使用此代码,我得到的输出为:

输出: 文档的根元素:学生

学生总数:4人 姓名:约翰 等级:B

年龄:12 姓名:玛丽 成绩:A

年龄:11 姓名:西蒙 成绩:A

年龄:18 姓名:Rasika 成绩:A+ 年龄:23

我想将上述输出存储到MySQL中。有人能帮忙吗

  • 确保您有用于MySQL的JDBC驱动程序jar,无论是从依赖项管理器还是使用依赖项管理器
  • 从java连接

    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public class XMLParser {
    
        public void getAllUserNames(String fileName) {
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                File file = new File(fileName);
                if (file.exists()) {
                    Document doc = db.parse(file);
                    Element docEle = doc.getDocumentElement();
    
                    // Print root element of the document
                    System.out.println("Root element of the document: "
                            + docEle.getNodeName());
    
                    NodeList studentList = docEle.getElementsByTagName("student");
    
                    // Print total student elements in document
                    System.out
                            .println("Total students: " + studentList.getLength());
    
                    if (studentList != null && studentList.getLength() > 0) {
                        for (int i = 0; i < studentList.getLength(); i++) {
    
                            Node node = studentList.item(i);
    
                            if (node.getNodeType() == Node.ELEMENT_NODE) {
    
                                System.out
                                        .println("=====================");
    
                                Element e = (Element) node;
                                NodeList nodeList = e.getElementsByTagName("name");
                                System.out.println("Name: "
                                        + nodeList.item(0).getChildNodes().item(0)
                                                .getNodeValue());
    
                                nodeList = e.getElementsByTagName("grade");
                                System.out.println("Grade: "
                                        + nodeList.item(0).getChildNodes().item(0)
                                                .getNodeValue());
    
                                nodeList = e.getElementsByTagName("age");
                                System.out.println("Age: "
                                        + nodeList.item(0).getChildNodes().item(0)
                                                .getNodeValue());
                            }
                        }
                    } else {
                        System.exit(1);
                    }
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        public static void main(String[] args) {
    
            XMLParser parser = new XMLParser();
            parser.getAllUserNames("C:/Users/rasika_patil01/Desktop/205_008_JAXBTesterOld/Student.xml");
        }
    }
    
  • 使用实际的数据库连接参数以及数据库表名、列名和列类型

    MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setUser(user);
    dataSource.setPassword(password);
    dataSource.setServerName(server);
    dataSource.setDatabaseName(database);
    Connection conn = dataSource.getConnection();
    
    PreparedStatement stmt = conn.prepareStatement("INSERT INTO table (name, grade, age) VALUES (?, ?, ?)");
    
    ...then while going throug the XML:
    
    stmt.setString(1, name);
    stmt.setString(2, grade);
    stmt.setString(3, age);
    stmt.execute();
    
    ...at the end:
    stmt.close();
    conn.commit();
    conn.close()