如何在数据库中读取和存储XML数据 [英] How to read and store XML data in database

查看:159
本文介绍了如何在数据库中读取和存储XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am very new joiner in java and xml parser, I need to read data from an XMl file and store it in database.

首先,我要做的是从Xml文件中读取数据,并将其以列和行格式存储在文本文件中.谁能告诉我如何从xml文件中读取数据并以列和行的形式处理数据.

Firstly what I did is I have read data from an Xml fileand will store in in text file in Column and row format. Can any one please tell me how can read data from an xml file and manipulate data in form of column and rows.

列和行表示:

  1. 节点名称必须在行头中依次显示在标题位置

  1. The Node names must be displayed in header postion in row wise one after the other

示例:

Column1, column2, column3.....

  • 该数据中的值必须存储在以上各列的明智标签中

  • The values in that data must be stored in the above columns wise tags

    示例:

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

  • 请帮助我,过去10天来我一直在与之抗争,我无法逐行读取数据.我所做的是:

    Please help me, I am fight with this from past 10 days, I am unable to read data in column and row wise. What I have done is:

    示例代码:

      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();
            }
    
        }
        }
    

    推荐答案

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

    In order to form a query you should use 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();
    

    这篇关于如何在数据库中读取和存储XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆