对于一个数据量大、复杂的xml文件,要进行解析并且存入数据库。首先要对xml文件的dom结构有所了解,并进行分析,然后获取自己想要的数据,之后进行数据的存储。
接下来说明一下具体的步骤和方法。
一、解析xml文件
我的xml文件(这里只是列出了一部分)如下:
- <?xml version="1.0" encoding="utf-8"?>
- <person>
- <personID>22097</personID>
- <FullName>Ajay Gupta</FullName>
- <FirstName>Ajay</FirstName>
- <LastName>Gupta</LastName>
- <publication>
- <title>Explanation-based Failure Recovery</title>
- <year>1987</year>
- <authors>Ajay Gupta</authors>
- <jconf>AAAI</jconf>
- <id>13048</id>
- <label>0</label>
- <organization>null</organization>
- </publication>
- <publication>
- <title>Time Representation in Prolog Circuit Modelling</title>
- <year>1991</year>
- <authors>Yossi Lichtenstein,Bob Welham,Ajay Gupta</authors>
- <jconf>ALPUK</jconf>
- <id>39153</id>
- <label>0</label>
- <organization>null</organization>
- </publication><pre name="code" class="html"></person>
我想要的数据是publication中的id和authors,并且将id和authors都存为String格式,我的解析代码如下:
我创建了一个publication类,方便将数据存储:
Publication类:
- public class Publication {
- private String id;
- private String authors;
- public String getAuthors() {
- return authors;
- }
- public void setAuthors(String authors) {
- this.authors = authors;
- }
- public String getId() {
- return id;
- }
- public void setId(String string) {
- this.id = string;
- }
- @Override
- public String toString(){
- return this.id+":"+this.authors;
- }
- }
解析代码:
- private static ArrayList<Publication> getParserAuthor() {
- ArrayList<Publication> list= new ArrayList<Publication>();
- //获取DOM解析器
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder;
- try {
- builder = factory.newDocumentBuilder();
- Document doc;
- doc = builder.parse(new File("Ajay Gupta.xml"));
- //得到一个element根元素,获得根节点
- Element root = doc.getDocumentElement();
- System.out.println("根元素:"+root.getNodeName());
- //子节点
- NodeList personNodes = root.getElementsByTagName("publication");
- for(int i = 0; i<personNodes.getLength();i++){
- Element personElement = (Element) personNodes.item(i);
- Publication publication = new Publication();
- NodeList childNodes = personElement.getChildNodes();
- //System.out.println("*****"+childNodes.getLength());
- for (int j = 0; j < childNodes.getLength(); j++) {
- if(childNodes.item(j).getNodeType()==Node.ELEMENT_NODE){
- if("authors".equals(childNodes.item(j).getNodeName())){
- publication.setAuthors(childNodes.item(j).getFirstChild().getNodeValue());
- }else if("id".equals(childNodes.item(j).getNodeName())){
- publication.setId(childNodes.item(j).getFirstChild().getNodeValue());
- }
- }
- }
- list.add(publication);
- }
- for(Publication publication2 : list){ //为了查看数据是否正确,进行打印结果
- System.out.println(publication2.toString());
- }
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- }
- return list;
- }
1)我创建的数据库表结构为:
Publication表:
设置id的目的是让数据能够自增,记得要设置为主键并且是不为空。如果没有客户端可以创建数据库和表,可以直接在命令行里写也是很简单的。
2)插入数据库。
在进行数据库操作时,需要导入连接数据库的驱动,并测试是否连接成功。
- static String sqlStr = "jdbc:mysql://localhost:3306/Publications";
- static String rootName = "root";//数据库名
- static String rootPwd = "root";//数据库密码
- public static void writeToMysql(Publication publication) {
- System.out.println(publication);
- //1.加载driver驱动
- try {
- // 加载MySql的驱动类
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("找不到驱动程序类 ,加载驱动失败!");
- e.printStackTrace();
- }
- //2.建立连接
- Statement st = null;
- //调用DriverManager对象的getConnection()方法,获得一个Connection对象
- Connection con =null;
- try {
- //建立数据库连接
- con = DriverManager.getConnection(sqlStr, rootName,rootPwd);
- //INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
- String paperId= publication.getId();
- String authors = publication.getAuthors();
- //插入语句格式
- String sql = "insert into publication1(paperId,Authors) values(\""+paperId+"\",\""+authors+"\")";
- System.out.println(sql);
- st = con.createStatement(); //创建一个Statement对象
- st.executeUpdate(sql);//提交数据更新
- } catch (SQLException e) {
- e.printStackTrace();
- }finally{
- try {
- st.close();
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- ArrayList<Publication> list= new ArrayList<Publication>();
- list = getParserAuthor();
- for(int i=0;i<list.size();i++)
- {
- if (list.get(i)!=null)
- writeToMysql(list.get(i));
- }
- }