如何使用xml做中间数据传输来绑定数据,实现增删改等操作

witcheryne 2008-09-21 12:20:58
具体实现结构如下:
JDBC - > 将数据转化成 xml 文件-> 页面通过读取xml来显示数据...
页面显示用的是XLoadTree, 只能使用xml做数据源...
我想是想对XLoadTree的增加删除等功能... 请问各位都是怎么实现的...
...全文
113 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Angelasunny 2012-01-07
  • 打赏
  • 举报
回复
//初始化
private void btnInitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInitActionPerformed
// TODO add your handling code here:
try {
File file = new File("books.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.
newInstance();
DocumentBuilder builder = docFactory.newDocumentBuilder();

if (!file.exists()) {
doc = builder.newDocument();
Element books = doc.createElement("books");
doc.appendChild(books);
} else {
doc = builder.parse(file);
}
this.jTextArea1.setText("初始化完成");
} catch (Exception ex) {
this.jTextArea1.setText("初始化失败");
}
}//GEN-LAST:event_btnInitActionPerformed

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
// TODO add your handling code here:
try {
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("books.xml"));

transformer.transform(source, result);

this.jTextArea1.setText("保存成功");
} catch (Exception ex) {
this.jTextArea1.setText("保存失败");
}
}//GEN-LAST:event_btnSaveActionPerformed

private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowActionPerformed
// TODO add your handling code here:
if (doc != null) {
Element books = doc.getDocumentElement();
this.jTextArea1.setText(books.getNodeName());

NodeList nl = null;
nl = books.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
//节点类型,元素1/属性2/文本3/注释8/文档9
if (node.getNodeType() == 1) {
this.jTextArea1.append("\n\t" + node.getNodeName());
NodeList nsubs = node.getChildNodes();
for (int j = 0; j < nsubs.getLength(); j++) {
if (nsubs.item(j).getNodeType() == 1 &&
nsubs.item(j).getFirstChild() != null) {
this.jTextArea1.append("\n\t\t" +
nsubs.item(j).getNodeName() + " : " +
nsubs.item(j).getFirstChild().
getNodeValue());
}
}
}
}
}
}//GEN-LAST:event_btnShowActionPerformed

private void btnShow2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShow2ActionPerformed
// TODO add your handling code here:
String msg = "";
if (doc != null){
NodeList nl = doc.getElementsByTagName("*");

for (int i=0;i<nl.getLength();i++){
Element e = (Element)nl.item(i);

//得到属性
NamedNodeMap nnm = e.getAttributes();
msg += "元素:" + e.getNodeName() +":" + e.getFirstChild().getNodeValue() + "\n";

for (int k=0;k<nnm.getLength();k++){
Attr att = (Attr)nnm.item(k);
msg += "\t属性有\t"+ att.getNodeName() + ":" + att.getNodeValue()+"\n";
}
}
}
this.jTextArea1.setText(msg);
}//GEN-LAST:event_btnShow2ActionPerformed

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
// TODO add your handling code here:
Node node = Search(this.jTextField1.getText());
if (node != null) {
this.jTextArea1.setText("找到了此元素");
this.jTextArea1.append("\n\t书籍编号 \t: " + this.jTextField1.getText());
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node sub = list.item(i);
if (sub.getNodeType() == 1 && sub.getFirstChild() != null) {
this.jTextArea1.append("\n\t" + sub.getNodeName() + " \t: " +
sub.getFirstChild().getNodeValue());
}
}
}
else {
this.jTextArea1.setText("找不到此元素");
}
}//GEN-LAST:event_btnSearchActionPerformed

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
// TODO add your handling code here:

try {
//创建元素
Element elemBook = doc.createElement("book");
//添加到根元素底下
doc.getDocumentElement().appendChild(elemBook);

//为book元素设置属性
Attr attrId = doc.createAttribute("id"); //创建
attrId.setNodeValue(this.jTextField1.getText()); //设置值
elemBook.setAttributeNode(attrId); //设置到book元素

//为book元素添加子元素name
Element elemNode = doc.createElement("name"); //创建
Text textNode = doc.createTextNode(this.jTextField2.getText()); //创建文本节点
elemNode.appendChild(textNode); //为name子元素指定文本
elemBook.appendChild(elemNode); //添加为book的子元素

//为book元素添加子元素price
elemNode = doc.createElement("price"); //创建
textNode = doc.createTextNode(this.jTextField3.getText()); //创建文本节点
elemNode.appendChild(textNode); //为price子元素指定文本
elemBook.appendChild(elemNode); //添加为book的子元素

//为book元素添加子元素pub
elemNode = doc.createElement("pub"); //创建
textNode = doc.createTextNode(this.jTextField4.getText()); //创建文本节点
elemNode.appendChild(textNode); //为pub子元素指定文本
elemBook.appendChild(elemNode); //添加为book的子元素

//为book元素添加子元素author
elemNode = doc.createElement("author"); //创建
textNode = doc.createTextNode(this.jTextField5.getText()); //创建文本节点
elemNode.appendChild(textNode); //为author子元素指定文本
elemBook.appendChild(elemNode);

this.jTextArea1.setText("添加成功");
} catch (Exception ex) {
this.jTextArea1.setText("添加失败");
}
}//GEN-LAST:event_btnAddActionPerformed

private void btnModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifyActionPerformed
// TODO add your handling code here:
Node node = Search(this.jTextField1.getText());
String[] values = {this.jTextField2.getText(), this.jTextField3.getText(),
this.jTextField4.getText(), this.jTextField5.getText()};
if (node != null) {
NodeList nl = node.getChildNodes();
int k = 0;
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
if (n.getNodeType() == 1) {
Node newNode = doc.createTextNode(values[k]);
if (n.getFirstChild() != null) {
n.replaceChild(newNode, n.getFirstChild());
} else {
n.appendChild(newNode);
}
k = k + 1;
}
}
this.jTextArea1.setText("修改成功");
} else {
this.jTextArea1.setText("找不到要修改的节点");
}
}//GEN-LAST:event_btnModifyActionPerformed

private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelActionPerformed
// TODO add your handling code here:
Node node = Search(this.jTextField1.getText());
if (node != null) {
Node parent = node.getParentNode();
parent.removeChild(node);

this.jTextArea1.setText("删除成功");
} else {
this.jTextArea1.setText("找不到要删除的节点");
}
}//GEN-LAST:event_btnDelActionPerformed

private Node Search(String id) {
Node node = null;
Element books = doc.getDocumentElement();
NodeList nl = null;
if (books.hasChildNodes()) {
nl = books.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
node = nl.item(i);
if (node.getNodeType() == 1) {
Element ele = (Element) node;
if (ele.getAttribute("id").equals(id)) {
return node;
}
}
}
}
return null;
}



witcheryne 2008-10-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 rainsilence 的回复:]
读xml最慢的是dom
jdk1.6以前最快的是sax2.0
我推荐jdk1.6得方法jaxb2.0
不管是dom,dom4j还是sax2.0,还是原生支持最快

Java codepackage org.school;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAttribute;
@XmlRootElement
public class Person {

private String name = null;

public String getName() {
return name;
}


[/Quote]
多谢,, 以前用的是Jdom , 没考虑过效率的问题...
学习了....

witcheryne 2008-10-02
  • 打赏
  • 举报
回复
好多没来了..
现在这里谢过各位...
witcheryne 2008-10-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dsn21 的回复:]
用js 也是就可以实现的 而且移植性很好
[/Quote]
最近看了些AJax方面的东西.... 基本上知道怎么做了...
webpage -> servlet - > xml - > webpage(使用XMLHttpRequest获取请求数据中的xml,使用js进行解析...)
好像是用DWR更方便..正在研究中..
kongwei302 2008-09-26
  • 打赏
  • 举报
回复
嗯 就是这么简单明了
rainsilence 2008-09-26
  • 打赏
  • 举报
回复
把读出来的对象转成javabean,再用jaxb2画成xml最快
rainsilence 2008-09-26
  • 打赏
  • 举报
回复
读xml最慢的是dom
jdk1.6以前最快的是sax2.0
我推荐jdk1.6得方法jaxb2.0
不管是dom,dom4j还是sax2.0,还是原生支持最快
package org.school;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAttribute;
@XmlRootElement
public class Person {

private String name = null;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

private int age = 0;

@XmlAttribute
public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}


package org.school;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//FileOutputStream os = new FileOutputStream("d:\\a.xml");
JAXBContext context = JAXBContext.newInstance(Person.class);
Person person = new Person();
person.setName("123");
Marshaller shaller = context.createMarshaller();
shaller.marshal(person, System.out);
/*FileInputStream is = new FileInputStream("d:\\a.xml");
Unmarshaller unShaller = context.createUnmarshaller();
Person instance = (Person)unShaller.unmarshal(is);
System.out.println(instance.getName());*/
} catch(JAXBException e) {
e.printStackTrace();
} /*catch(IOException e) {
e.printStackTrace();
}*/

}
}


dsn21 2008-09-26
  • 打赏
  • 举报
回复
用js 也是就可以实现的 而且移植性很好
tyhAaron 2008-09-26
  • 打赏
  • 举报
回复
怎么没人帮顶啊 兄弟我顶你
jinxinzhang 2008-09-22
  • 打赏
  • 举报
回复
java对xml操作,就自己的体会来说,如何你的树状结构不是很复杂的话,用XStream来对java文件的操作比较简单.
下载一个jar包,创建需要序列化的类。操作起来比较简单.

在和其他系统做接口时候,曾经遇到到过一个十分复杂的一个树状的xml,最后还是用的老办法dom4j来解析和操作.

具体的例子,网上都有,如果你需要的话,可以发一份给你

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧