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 三羽 阅读(579) 评论(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>



只有注册用户登录后才能发表评论。


网站导航: