从xml中提取数据后,数据必须存储在SQL数据库中

时间:2014-03-16 13:53:22

标签: java sql sql-server xml nodelist

我编写了Java程序,它从xml中提取数据并提供以下输出。 提取完成后,数据必须存储在SQL数据库中。请帮我在SQL数据库中添加数据,

以下输出必须存储在数据库中,

输出: -

Root element of the doc is geodata
Total no of people : 2
Address : 2344 States Drive, MA 01213, USA
Phone Number : 333-222-2222
Salary : $3000
Pension : $3000
Address : 2345 Gates Drive, PA 11213, USA
Phone Number : 444-222-2222
Salary : $3500
Pension : $3500

源代码: -

package com.techassignment;

import java.io.File;

import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 

public class readxml 
{


        public static void main (String argv [])
        {
        try {

                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                Document doc = docBuilder.parse(new File("C:\\Users\\mkar\\workspace\\techassignment - new\\src\\com\\techassignment\\testFinal.xml"));

                // normalize text representation
                doc.getDocumentElement ().normalize ();
                System.out.println ("Root element of the doc is " + 
                     doc.getDocumentElement().getNodeName());


                NodeList listOfPersons = doc.getElementsByTagName("person");
                int totalPersons = listOfPersons.getLength();
                System.out.println("Total no of people : " + totalPersons);

                for(int s=0; s<listOfPersons.getLength() ; s++){


                    Node firstPersonNode = listOfPersons.item(s);
                    if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


                        Element firstPersonElement = (Element)firstPersonNode;

                        //-------
                        NodeList addressList = firstPersonElement.getElementsByTagName("address");
                        Element addressElement = (Element)addressList.item(0);

                        NodeList textFNList = addressElement.getChildNodes();
                        System.out.println("Address : " + 
                               ((Node)textFNList.item(0)).getNodeValue().trim());

                        //-------
                        NodeList phonenumberList = firstPersonElement.getElementsByTagName("phonenumber");
                        Element phonenumberElement = (Element)phonenumberList.item(0);

                        NodeList textLNList = phonenumberElement.getChildNodes();
                        System.out.println("Phone Number : " + 
                               ((Node)textLNList.item(0)).getNodeValue().trim());

                        //----
                        NodeList salaryList = firstPersonElement.getElementsByTagName("salary");
                        Element salaryElement = (Element)salaryList.item(0);

                        NodeList textAgeList = salaryElement.getChildNodes();
                        System.out.println("Salary : " + 
                               ((Node)textAgeList.item(0)).getNodeValue().trim());

                        //----
                        NodeList PensionList = firstPersonElement.getElementsByTagName("pension");
                        Element PensionElement = (Element)salaryList.item(0);

                        NodeList textPensionList = salaryElement.getChildNodes();
                        System.out.println("Pension : " + 
                               ((Node)textPensionList.item(0)).getNodeValue().trim());
                        //------


                    }//end of if clause


                }//end of for loop with s var


            }catch (SAXParseException err) {
            System.out.println ("** Parsing error" + ", line " 
                 + err.getLineNumber () + ", uri " + err.getSystemId ());
            System.out.println(" " + err.getMessage ());

            }catch (SAXException e) {
            Exception x = e.getException ();
            ((x == null) ? e : x).printStackTrace ();

            }catch (Throwable t) {
            t.printStackTrace ();
            }
            //System.exit (0);

        }//end of main


}

1 个答案:

答案 0 :(得分:0)

创建一个方法说插入(root,noOfPeople,Address,phNo,salary)并将所有想要插入数据库的数据逐个传递给参数值,以便它按行插入

数据库中的第1行将是 -

   Address : 2344 States Drive, MA 01213, USA
   Phone Number : 333-222-2222
   Salary : $3000
   Pension : $3000

数据中的第2行将是 -

Address : 2345 Gates Drive, PA 11213, USA
Phone Number : 444-222-2222
Salary : $3500
Pension : $3500

如果你不知道如何存储数据,那么在java中读取JDBC。 你也可以试试 1. How to store data in Java? Database? 2. http://www.tutorialspoint.com/jdbc/jdbc-db-connections.htm 3。http://www.tutorialspoint.com/jdbc/jdbc-insert-records.htm