Believe it,do it!

          Ideal is the beacon. Without ideal, there is no secure direction; without direction ,there is no life.
          理想是指路明燈。沒有理想,就沒有堅定的方向;沒有方向,就沒有生活。
          CTRL+T eclipse
          posts - 35, comments - 3, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          數據庫xml互相操作!!!

          Posted on 2009-05-18 13:47 三羽 閱讀(593) 評論(0)  編輯  收藏 所屬分類: JAVA資料
          JAVA如何將XML文檔里的數據寫入oracle數據庫

          import org.w3c.dom.Document;
          import org.w3c.dom.Element;
          import org.w3c.dom.NodeList;
          import javax.xml.parsers.DocumentBuilderFactory;
          import javax.xml.parsers.DocumentBuilder;
          import javax.xml.parsers.ParserConfigurationException;
          import org.xml.sax.SAXException;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.Statement;
          import java.sql.SQLException;
          import java.io.IOException;

          public class test1
          {
           static Connection conn=null;
           static String deptno,dname,loc,sql;
           static String url="jdbc:oracle:oci8:@hydb";
           public static void main(String[] args)
           {
            try
            {
             Class.forName("oracle.jdbc.driver.OracleDriver");
             conn=DriverManager.getConnection(url,"scott","tiger");
             Statement st=conn.createStatement();
             
             DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
             DocumentBuilder builder=factory.newDocumentBuilder();
             Document doc=builder.parse("dept.xml");
             
             NodeList node=doc.getElementsByTagName("PERSON");
             System.out.println(node.getLength());
             
             for(int i=0;i<node.getLength();i++)
             {
              Element element=(Element)node.item(i);
              
              deptno=element.getElementsByTagName("DEPTNO").item(0).getFirstChild().getNodeValue();
              dname=element.getElementsByTagName("DNAME").item(0).getFirstChild().getNodeValue();
              loc=element.getElementsByTagName("LOC").item(0).getFirstChild().getNodeValue();
              
              sql="insert into person values('"+deptno+"','"+dname+"','"+loc+"')";
              
              st.executeUpdate(sql);
             }
             st.close();
             conn.close();
             System.out.println("插入成功!!!");
            }catch(ClassNotFoundException e)
            {
             e.printStackTrace();
            }catch(SQLException e1)
            {
             e1.printStackTrace();
            }catch(ParserConfigurationException e2)
            {
             e2.printStackTrace();
            }catch(SAXException e3)
            {
             e3.printStackTrace();
            }catch(IOException e4)
            {
             e4.printStackTrace();
            }
           }
          }

           

          dept.xml文件

          <?xml version="1.0" encoding="UTF-8"?>

          <COMP>
            <PERSON>
              <DEPTNO>10</DEPTNO>
              <DNAME>ACCOUNTING</DNAME>
              <LOC>NEW YORK</LOC>
            </PERSON>
            <PERSON>
              <DEPTNO>20</DEPTNO>
              <DNAME>RESEARCH</DNAME>
              <LOC>DALLAS</LOC>
            </PERSON>
            <PERSON>
              <DEPTNO>30</DEPTNO>
              <DNAME>SALES</DNAME>
              <LOC>CHICAGO</LOC>
            </PERSON>
            <PERSON>
              <DEPTNO>40</DEPTNO>
              <DNAME>OPERATIONS</DNAME>
              <LOC>BOSTON</LOC>
            </PERSON>
          </COMP>

           

          JAVA如何將oracle數據庫里的數據寫入XML文檔

          import org.w3c.dom.Document;
          import org.w3c.dom.Element;
          import org.apache.crimson.tree.XmlDocument;
          import javax.xml.parsers.DocumentBuilderFactory;
          import javax.xml.parsers.DocumentBuilder;
          import javax.xml.parsers.ParserConfigurationException;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.Statement;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import java.io.File;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.FileNotFoundException;
          import java.io.IOException;
          import org.xml.sax.SAXException;

          public class test
          {
           static Connection conn=null;
           static String sql;
           static String url="jdbc:oracle:oci8:@hydb";
           public static void main(String[] args)
           {
            try
            {
             Class.forName("oracle.jdbc.driver.OracleDriver");
             conn=DriverManager.getConnection(url,"scott","tiger");
             Statement st=conn.createStatement();
             ResultSet rs=st.executeQuery("select * from dept");
             
             DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
             DocumentBuilder builder=factory.newDocumentBuilder();
             Document doc=builder.newDocument();
             
             Element comp=doc.createElement("COMP");
             
             while(rs.next())
             {
              Element person=doc.createElement("PERSON");
              
              Element deptno=doc.createElement("DEPTNO");
              deptno.appendChild(doc.createTextNode(String.valueOf(rs.getInt(1))));
              person.appendChild(deptno);
              
              Element dname=doc.createElement("DNAME");
              dname.appendChild(doc.createTextNode(rs.getString(2)));
              person.appendChild(dname);
              
              Element loc=doc.createElement("LOC");
              loc.appendChild(doc.createTextNode(rs.getString(3)));
              person.appendChild(loc);
              
              comp.appendChild(person);
             }
             rs.close();
             st.close();
             conn.close();
             
             doc.appendChild(comp);
             
             ((XmlDocument)doc).write(new FileOutputStream(new File("dept.xml")));
             
             System.out.println("操作成功!!!");
            }catch(ClassNotFoundException e)
            {
             e.printStackTrace();
            }catch(SQLException e1)
            {
             e1.printStackTrace();
            }catch(ParserConfigurationException e2)
            {
             e2.printStackTrace();
            }catch(FileNotFoundException e3)
            {
             e3.printStackTrace();
            }catch(IOException e4)
            {
             e4.printStackTrace();
            }
           }
          }

          dept.xml文件

          <?xml version="1.0" encoding="UTF-8"?>

          <COMP>
            <PERSON>
              <DEPTNO>10</DEPTNO>
              <DNAME>ACCOUNTING</DNAME>
              <LOC>NEW YORK</LOC>
            </PERSON>
            <PERSON>
              <DEPTNO>20</DEPTNO>
              <DNAME>RESEARCH</DNAME>
              <LOC>DALLAS</LOC>
            </PERSON>
            <PERSON>
              <DEPTNO>30</DEPTNO>
              <DNAME>SALES</DNAME>
              <LOC>CHICAGO</LOC>
            </PERSON>
            <PERSON>
              <DEPTNO>40</DEPTNO>
              <DNAME>OPERATIONS</DNAME>
              <LOC>BOSTON</LOC>
            </PERSON>
          </COMP>
          JAVA如何將SQLSERVER數據庫里的數據寫入XML文檔

          import org.w3c.dom.Document;
          import org.w3c.dom.Element;
          import org.apache.crimson.tree.XmlDocument;
          import javax.xml.parsers.DocumentBuilderFactory;
          import javax.xml.parsers.DocumentBuilder;
          import javax.xml.parsers.ParserConfigurationException;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.Statement;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import java.io.File;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.FileNotFoundException;
          import java.io.IOException;
          import org.xml.sax.SAXException;

          public class test2
          {
           static Connection conn=null;
           static String sql;
           static String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs";
           public static void main(String[] args)
           {
            try
            {
             Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
             conn=DriverManager.getConnection(url,"sa","");
             Statement st=conn.createStatement();
             ResultSet rs=st.executeQuery("select * from jobs");
             
             DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
             DocumentBuilder builder=factory.newDocumentBuilder();
             Document doc=builder.newDocument();
             
             Element comp=doc.createElement("COMP");
             
             while(rs.next())
             {
              Element person=doc.createElement("PERSON");
              
              Element job_id=doc.createElement("JOB_ID");
              job_id.appendChild(doc.createTextNode(String.valueOf(rs.getInt(1))));
              person.appendChild(job_id);
              
              Element job_desc=doc.createElement("JOB_DESC");
              job_desc.appendChild(doc.createTextNode(rs.getString(2)));
              person.appendChild(job_desc);
              
              Element min_lvl=doc.createElement("MIN_LVL");
              min_lvl.appendChild(doc.createTextNode(String.valueOf(rs.getInt(3))));
              person.appendChild(min_lvl);
              
              Element max_lvl=doc.createElement("MAX_LVL");
              max_lvl.appendChild(doc.createTextNode(String.valueOf(rs.getInt(4))));
              person.appendChild(max_lvl);
              
              comp.appendChild(person);
             }
             rs.close();
             st.close();
             conn.close();
             
             doc.appendChild(comp);
             
             ((XmlDocument)doc).write(new FileOutputStream(new File("jobs.xml")));
             
             System.out.println("操作成功!!!");
            }catch(ClassNotFoundException e)
            {
             e.printStackTrace();
            }catch(SQLException e1)
            {
             e1.printStackTrace();
            }catch(ParserConfigurationException e2)
            {
             e2.printStackTrace();
            }catch(FileNotFoundException e3)
            {
             e3.printStackTrace();
            }catch(IOException e4)
            {
             e4.printStackTrace();
            }
           }
          }

          java解析xml字符串
          import java.util.*;
          import test.*;

          public class MyDOMBean implements java.io.Serializable {
                  Vector vec = new Vector();

                    public MyDOMBean() {
                     }

            public Vector getMappingValueNode(String strIndex,String strArea)
              {
                  NodeIterator keys = null;
                  Document doc = null;
                  try
                  {
                      DocumentBuilderFactory factory =
                              DocumentBuilderFactory.newInstance();
                      DocumentBuilder builder = factory.newDocumentBuilder();
                      doc = builder.parse("/opt/tomcat/jakarta-tomcat-5.0.28/webapps/Visual/WEB-INF/qa.xml");
                      String xpath = "/root/" + strArea + "/*";
                      keys = XPathAPI.selectNodeIterator(doc, xpath);

                          Node key = null;

                          int i = 0;
                          
                          
                          while ((key = keys.nextNode()) != null)
                          {
                              NodeIterator aKey = null;
                              NodeIterator pKey = null;
                              Node name = key.getFirstChild();
                              name = name.getNextSibling();
                              QuesAnswer qa = new QuesAnswer();
                              String tempQ = null;
                              tempQ = name.getFirstChild().getNodeValue().toString();
                              name = name.getNextSibling();
                              name = name.getNextSibling();
                              String strTempA = "./answer";
                              aKey = XPathAPI.selectNodeIterator(key,strTempA);
                              Node tempKeyA = null;
                                     ArrayList listA = new ArrayList();
                              String tempP = null;
                              while((tempKeyA = aKey.nextNode()) != null){
                                      listA.add(tempKeyA.getFirstChild().getNodeValue().toString());
                              }

                              String[] tempA = new String[listA.size()];
                              for(int l = 0;l<listA.size();l++){
                                      tempA[l] = (String)listA.get(l);
                              }
                              String strTempP = "./pic";
                              pKey = XPathAPI.selectNodeIterator(key,strTempP);
                              Node tempKeyP = null;
                              while((tempKeyP = pKey.nextNode()) !=null){
                                      tempP = tempKeyP.getAttributes().getNamedItem("file").getNodeValue();
                              }
                                                      
                             
                              if(strIndex==null||strIndex.equals("")){
                                      qa.setQuestion(tempQ);
                                      qa.setAnswer(tempA);
                                      qa.setPicture(tempP);
                                      vec.add(qa);
                                    }else{
                                            if (tempQ.indexOf(strIndex)>= 0){
                                              qa.setQuestion(tempQ);
                                              qa.setAnswer(tempA);
                                              qa.setPicture(tempP);                                   
                                              vec.add(qa);
                                      }else{
                                              for(int h = 0;h<tempA.length;h++){
                                                      if(tempA[h].indexOf(strIndex)>=0){
                                                              qa.setQuestion(tempQ);
                                                              qa.setAnswer(tempA);
                                                              qa.setPicture(tempP);
                                                              vec.add(qa);
                                                              break;
                                                      }               
                                              }
                                      }
                              }
                             
                           }
                   
                  
                  }catch(Exception ex)
                  {
                      System.out.println("[Error] Can not get mapping Value.");
                      System.out.println(ex);
                  }
                          return vec;
                         
              }

          }
          2、
          package test;

          public class QuesAnswer{
                  private String question = null;
                  private String[] answer = null;
                  private String picture = null;

                  public void setQuestion(String ques){
                          this.question = ques;
                  }
                 
                  public void setAnswer(String[] answer){
                          this.answer = answer;
                  }

                  public String getQuestion(){
                          return this.question;
                  }
                 
                  public String[] getAnswer(){
                          return this.answer;
                  }
                 
                  public void setPicture(String picture){
                          this.picture = picture;
                  }
                 
                  public String getPicture(){
                          return this.picture;
                  }
          }
          3、
          XML



          <?xml version="1.0" encoding="GB2312" ?>
          <root>
          <resume1>
          <qa id="01">
             <question>問題:我的PDA不能聯結到靈視系統服務器。
          </question>
             <answer>回答:請檢查是否已經播號聯結(移動用戶撥,聯通用戶撥#777,小靈通用戶撥ISP服務號碼)。
          </answer>  
          </qa>  
          <qa id="02">
             <question>問題:我的PC不能聯結到靈視系統服務器。ssssss
             </question>
             <answer>回答:請確認;
                  1. 網線已連接且工作正常。
                  2. MPEG-4_Player目錄下Decoder.ini文件中ip地址和端口號是否正確。
                  </answer>  
          </qa>   
          <qa id="03">
             <question>問題:</question>
             <answer>回答1:</answer>  
             <answer>回答2:</answer>  
             <answer>回答3:</answer>  
             <answer>回答4:</answer>  
             <answer>回答5:</answer>
             <pic file="jiejuefangan-1.jpg"/>
          </qa>   
          <qa id="04">
             <question>問題:</question>
             <answer>回答:</answer>  
          </qa>   
          </resume1>

          <resume2>
          <qa id="01">
             <question>問題:</question>
             <answer>回答:</answer>  
          </qa>  
          <qa id="02">
             <question>問題:</question>
             <answer>回答:</answer>
             <answer>回答1:</answer>  
             <answer>回答2:</answer>  
             <answer>回答3:</answer>  
             <answer>回答4:</answer>  
             <answer>回答5:</answer>
          </qa>   
          <qa id="03">
             <question>問題:</question>
             <answer>回答:</answer>  
          </qa>   
          <qa id="04">
             <question>問題:</question>
             <answer>回答:</answer>  
          </qa>   
          </resume2>
          </root>


          主站蜘蛛池模板: 怀安县| 固原市| 富蕴县| 建昌县| 嘉黎县| 襄城县| 巴彦县| 黑河市| 屯门区| 浪卡子县| 都匀市| 洞头县| 丁青县| 宁德市| 会理县| 肃宁县| 光山县| 宽甸| 瑞安市| 晴隆县| 锦州市| 德江县| 石屏县| 荔波县| 莫力| 康马县| 神木县| 疏勒县| 长寿区| 岫岩| 台北市| 乌苏市| 灵川县| 江都市| 城市| 安乡县| 喀喇沁旗| 日喀则市| 洞口县| 肥乡县| 汝阳县|