<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 三羽 閱讀(591) 評論(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>


    主站蜘蛛池模板: 久久av免费天堂小草播放| 亚洲综合无码一区二区痴汉| 日韩a在线观看免费观看| 100部毛片免费全部播放完整| 一级做a爰片久久毛片免费陪| 亚洲熟女综合一区二区三区| 亚洲国产亚洲片在线观看播放 | 一本久久a久久精品亚洲| 又粗又硬免费毛片| 国产成人免费A在线视频| 西西大胆无码视频免费| 无码国产精品一区二区免费I6| 精品久久8x国产免费观看| 日本成年免费网站| 成年性午夜免费视频网站不卡| 99久久久精品免费观看国产 | 亚洲国产一级在线观看| 亚洲精品动漫人成3d在线| 国产亚洲精品久久久久秋霞| 亚洲色偷拍另类无码专区| 亚洲AV无码成人精品区在线观看| 亚洲av无码成h人动漫无遮挡| 亚洲视频中文字幕在线| 亚洲暴爽av人人爽日日碰| 色老头综合免费视频| 国产婷婷成人久久Av免费高清| 久章草在线精品视频免费观看| 麻花传媒剧在线mv免费观看| 日韩高清在线免费观看| 亚洲一区二区三区自拍公司| 亚洲91精品麻豆国产系列在线| 青青免费在线视频| 久久青青草原国产精品免费| 女人张开腿等男人桶免费视频| 国产gav成人免费播放视频| 亚洲AV日韩AV永久无码久久 | 日韩免费观看的一级毛片| 亚洲午夜久久久久妓女影院| 亚洲乱码无人区卡1卡2卡3| 久操免费在线观看| 亚洲熟妇无码八AV在线播放|