java 服务器端扫描文件夹下xml文件,并导入到Oracle数据库

老师让用jsp做一个B/S架构的项目,其中有个功能是服务器自动扫描某个文件夹里是否有xml的文件,如果有,把里面的数据导入到Oracle里。求哥哥姐姐帮个忙,最好能来点代... 老师让用jsp做一个B/S架构的项目,其中有个功能是服务器自动扫描某个文件夹里是否有xml的文件,如果有,把里面的数据导入到Oracle里。求哥哥姐姐帮个忙,最好能来点代码^_^ 展开
 我来答
1997排长
高粉答主

2016-01-10 · 说的都是干货,快来关注
知道大有可为答主
回答量:1.1万
采纳率:0%
帮助的人:813万
展开全部
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class CreateXMLForEnterServelet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";

//Initialize global variables
public void init() throws ServletException {
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
//获取传递过来的值
String databaseType = request.getParameter("select");
String userName = request.getParameter("userName");
String userPassword = request.getParameter("userPassword");
String dbName = request.getParameter("dbName");
String dbSouse = request.getParameter("dbSouces");
int port = Integer.parseInt(request.getParameter("portNum"));
Document doc;
Element root;
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String path = request.getRealPath("/");
File file = new File( path + "/" + "DBInfo.xml");
if(!file.exists())
{
doc = db.newDocument();
root = doc.createElement("DBInfo");
doc.appendChild(root);
}
else
{
doc = db.parse(file);
root = doc.getDocumentElement();
}

//添加子节点
Element rowNode = doc.createElement(databaseType);
root.appendChild(rowNode);
Node node = doc.createElement("Driver");
Node nodestr = doc.createElement("ConnectString");
Node serverName = doc.createElement("ServerName");
Node nodeName = doc.createElement("UserName");
Node nodePWD = doc.createElement("PassWord");
Node nodePort = doc.createElement("Port");
Node nodeDB = doc.createElement("DataBase");

rowNode.appendChild(node);
rowNode.appendChild(nodestr);
rowNode.appendChild(serverName);
rowNode.appendChild(nodeDB);
rowNode.appendChild(nodeName);
rowNode.appendChild(nodePWD);
rowNode.appendChild(nodePort);

String nodeValue;//Driver
String nodeConStr;//连接字符串
if(databaseType.equals("ORACLE"))
{
nodeValue = "oracle.jdbc.driver.OracleDriver";
nodeConStr = "jdbc:oracle:thin";
}
else
{
nodeValue = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
nodeConStr = "jdbc:microsoft:sqlserver";
}
Text text = doc.createTextNode(nodeValue);
Text textstr = doc.createTextNode(nodeConStr);
Text textName = doc.createTextNode(userName);
Text textPWD = doc.createTextNode(userPassword);
Text textServer = doc.createTextNode(dbName);
Text textPort = doc.createTextNode(String.valueOf(port));
Text textDB = doc.createTextNode(dbSouse);

node.appendChild(text);
nodestr.appendChild(textstr);
nodeName.appendChild(textName);
nodePWD.appendChild(textPWD);
serverName.appendChild(textServer);
nodePort.appendChild(textPort);
nodeDB.appendChild(textDB);

DOMSource doms = new DOMSource(doc);

StreamResult sr = null;

TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
sr = new StreamResult(file);
tf.transform(doms,sr);
out.println("<h1>Create XMLFile Is Ok!!</h1>");
request.getRequestDispatcher("index.jsp").forward(request,response);
}
catch(Exception ex)
{
out.println("<h1>生成 XML 配置文件时出现错误:</h1><br/><h4>"+ex+"</h4>");
out.println("<br/><br/>");
out.print("<a href='index.jsp'>【点这里回到首页】</a>");

}

out.close();

}

//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}

//Clean up resources
public void destroy() {
}
}
之卿飞B
2014-10-10 · TA获得超过1015个赞
知道小有建树答主
回答量:1086
采纳率:80%
帮助的人:518万
展开全部
//获取xml文件
File file = new File("文件路径");
if(file.exists()){
File[] fs=file.listFiles();
for (File file2 : fs) {
if(file2.isFile()&&file2.getName().endsWith(".xml")){
System.out.println("获取到xml文件"+file2.getName());
}
}
}
//解析xml
public static List<Student> parseXML() {
List<Student> stu = new ArrayList<Student>();
SAXReader parser = new SAXReader();// dom4j锟侥斤拷锟斤拷锟斤拷
Reader reader = null;
try {
reader = new FileReader("people.xml");
Document doc = parser.read(reader); // 锟斤拷xml锟斤拷锟截碉拷document锟斤拷锟斤拷锟斤拷
Element root = doc.getRootElement(); // 锟斤拷锟斤拷锟斤拷锟斤拷思锟斤拷锟斤拷薪锟斤拷锟�
List<Element> list = root.elements();// 锟矫碉拷锟斤拷一锟斤拷元锟截硷拷锟斤拷
// elementIterator()
for (Element people : list)// people
{
String eleName = people.getName();// 元锟斤拷锟斤拷
List<Attribute> attributes = people.attributes();// 锟斤拷锟皆的硷拷锟斤拷
// 锟矫碉拷people锟斤拷锟斤拷一锟斤拷元锟斤拷
Student student = new Student();
List<Element> peoplePros = people.elements();
for (Element pro : peoplePros) {
if (pro.attribute("id") != null) {
student.setId(Integer.parseInt(pro.attribute("id")
.getValue()));
}
String peopleProName = pro.getName();
String peopleProValue = new String(pro.getTextTrim());
student.setName(peopleProName);
student.setAge(peopleProValue);
// System.out.println(pro.getNodeType());
// System.out.println(peopleProName + "----dd:"
// + peopleProValue);
}
stu.add(student);
}
return stu;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//xml文件
<?xml version="1.0" encoding="UTF-8"?>
<stuList>
<student id="1001">
<title>李四</title>
<year>21</year>
</student>
<student id="1002">
<title>张三</title>
<year>42</year>
</student>
</stuList>
可以按需求解析xml,返回的是一个集合,然后添加到数据库吧
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
guolidi000
2015-12-28
知道答主
回答量:66
采纳率:0%
帮助的人:14.6万
展开全部
递推 - 读取 - 解析-插入
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式