【问题标题】:How to read and store XML data in database如何在数据库中读取和存储 XML 数据
【发布时间】:2012-07-23 11:13:08
【问题描述】:

我是 java 和 xml 解析器的新手,我需要从 XMl 文件中读取数据并将其存储在数据库中。

首先,我从 Xml 文件中读取了数据,并将以列和行格式存储在文本文件中。谁能告诉我如何从 xml 文件中读取数据并以列和行的形式操作数据。

列和行表示:

  1. 节点名称必须逐行显示在标题位置

    例子:

    Column1, column2, column3.....
    
  2. 该数据中的值必须存储在上述列标记中

    例子:

    Column1, column2, column3,...  
    row1, row2, row3,...  
    row1, row2, row3,....  
    

请帮助我,过去 10 天我一直在与此作斗争,我无法读取列和行中的数据。我所做的是:

示例代码:

  public class XMLReader extends XMLUtil{

    public static void main(String args[]) {
        try{
        XMLUtil xml=new XMLUtil();
        Document doc=xml.docFromFilePath("data/Schools.xml");
        //System.out.println("Root element :"+ doc.getDocumentElement().getNodeName());
        NodeList nl= doc.getElementsByTagName("*");
        for(int i=0;i< nl.getLength();i++)
            {
                /*String column1=nl.item(i).getNodeName();
                String column=nl.item(i).getFirstChild().getNodeValue();
                System.out.println(column1+":"+ column);*/
            Node n = nl.item(i);
            String name=n.getNodeName();
            System.out.print(name+ " ");
            }
        System.out.println();




         File f=new File("results/test1.txt");
        xml.prettyPrintToFile(f, doc);
        FileUtil futil=new FileUtil();
        futil.readFileBytes(f);
        //System.out.println(futil.toString());
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

    }
    }

【问题讨论】:

  • 取你得到的值并构造一些 INSERT 语句。
  • 首先我想将数据存储到文本文件中。要将来自 dom 对象的数据修改为逐行,我该怎么做
  • 如何存储元素节点的数据以插入值

标签: java oracle xml-parsing


【解决方案1】:

为了形成查询,您应该使用PreparedStatement

示例用法:

String query = "INSERT INTO some_table (col1,col2,col3) values (?,?,?)
PreparedStatement stmt = null;
stmt = con.prepareStatement(query);
//now use the values from the xml and bind them to the statement
stmt.setString(someStringValue);
stmt.setInt(someIntValue);
...
stmt.execute();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    相关资源
    最近更新 更多