<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>


    主站蜘蛛池模板: CAOPORM国产精品视频免费| 亚洲精品无码专区| A国产一区二区免费入口| 国产成人涩涩涩视频在线观看免费| 亚洲一区二区三区免费在线观看| 国产va在线观看免费| 亚洲AV无码成人网站久久精品大 | 有色视频在线观看免费高清在线直播 | 亚洲国产天堂久久综合网站| 无码A级毛片免费视频内谢| 久久青青草原亚洲AV无码麻豆| 精品免费tv久久久久久久| 亚洲av永久无码精品秋霞电影影院| a级毛片免费高清毛片视频| 久久亚洲精品中文字幕无码| 热re99久久6国产精品免费| 亚洲视频一区在线| 和日本免费不卡在线v| 亚洲熟妇AV一区二区三区浪潮| 永久黄网站色视频免费观看| 香蕉视频亚洲一级| 成人亚洲性情网站WWW在线观看| 久久毛片免费看一区二区三区| 亚洲老妈激情一区二区三区| 久久永久免费人妻精品下载 | 日韩亚洲国产二区| 好吊色永久免费视频大全| 亚洲AV无码1区2区久久| 无码人妻一区二区三区免费 | 精品免费视在线观看| 91亚洲视频在线观看| 在线观看91精品国产不卡免费| 一区二区三区在线免费观看视频 | 亚洲日本va在线观看| 国产福利免费观看| 中文字幕免费不卡二区| 亚洲va在线va天堂va手机| 男人的天堂亚洲一区二区三区 | 亚洲男人天堂影院| 国产免费观看黄AV片| 久久亚洲免费视频|