用DOM解析XML檔案並存入mysql資料庫
阿新 • • 發佈:2019-01-16
對於一個數據量大、複雜的xml檔案,要進行解析並且存入資料庫。首先要對xml檔案的dom結構有所瞭解,並進行分析,然後獲取自己想要的資料,之後進行資料的儲存。
接下來說明一下具體的步驟和方法。
一、解析xml檔案
我的xml檔案(這裡只是列出了一部分)如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <person>
- <personID>22097</personID>
- <FullName>Ajay Gupta</FullName>
-
<FirstName
- <LastName>Gupta</LastName>
- <publication>
- <title>Explanation-based Failure Recovery</title>
- <year>1987</year>
- <authors>Ajay Gupta</authors>
- <jconf>AAAI</jconf>
-
<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
- <jconf>ALPUK</jconf>
- <id>39153</id>
- <label>0</label>
- <organization>null</organization>
- </publication><prename="code"class="html"></person>
我建立了一個publication類,方便將資料儲存:
Publication類:
- publicclass Publication {
- private String id;
- private String authors;
- public String getAuthors() {
- return authors;
- }
- publicvoid setAuthors(String authors) {
- this.authors = authors;
- }
- public String getId() {
- return id;
- }
- publicvoid setId(String string) {
- this.id = string;
- }
- @Override
- public String toString(){
- returnthis.id+":"+this.authors;
- }
- }
解析程式碼:
- privatestatic 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());
- }elseif("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";//資料庫密碼
- publicstaticvoid 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 {