写一个javaa程序,通过java程序读mysql数据库生成xml文档 25
这个是表字段,最好写详细点谢谢!!保底悬赏5,能用的话追加悬赏!!!没有能回答的吗,最高赏金100,要求注解详细,运行流畅,无错误!...
这个是表字段,最好写详细点谢谢!!保底悬赏5,能用的话追加悬赏!!!
没有能回答的吗,最高赏金100,要求注解详细,运行流畅,无错误! 展开
没有能回答的吗,最高赏金100,要求注解详细,运行流畅,无错误! 展开
2个回答
展开全部
package com.hibut.tool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.hibut.entity.Category;
import com.hibut.entity.ImageFile;
public class XMLEditor {
private File data;
private org.dom4j.Document doc;
public XMLEditor(String path) {
data = new File(path + "\\data.xml");
this.checkFile();
}
private void checkFile() {
if (!data.exists()) {
doc = DocumentHelper.createDocument();
doc.addComment("xml created at:" + new Date());
Element root = doc.addElement("data");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer;
try {
writer = new XMLWriter(new FileOutputStream(data), format);
writer.write(doc);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.getFile();
}
private void getFile() {
SAXReader reader = new SAXReader();
try {
doc = (Document) reader.read(data);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void saveDocument() {
XMLWriter writer = null;
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
try {
writer = new XMLWriter(new FileOutputStream(data), format);
writer.write(doc);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addImage(Category category) {
Element elt = null;
List list = doc.selectNodes("data/category[@name='" + category.getName() + "']");
if (list.size() > 0) {
elt = (Element) list.get(0);
Iterator iterator = category.getFiles().iterator();
ImageFile image = null;
while (iterator.hasNext()) {
image = (ImageFile) iterator.next();
Element tempElement = elt.addElement("file");
tempElement.addAttribute("name", image.getName());
tempElement.addAttribute("thumb", image.getThumb());
tempElement.addAttribute("file", image.getFileurl());
tempElement.addCDATA(image.getDescription());
}
this.saveDocument();
return;
}
// 添加的图片的图片分类不存在
elt = doc.getRootElement().addElement("category");// 创建分类
elt.addAttribute("name", category.getName());
elt.addAttribute("thumb", category.getThumb());
elt.addComment("category created at " + (new Date()).toString());
// 添加分类中的图片
if (category.getFiles().size() > 0) {
Iterator ite = category.getFiles().iterator();
while (ite.hasNext()) {
ImageFile image = (ImageFile) ite.next();
Element tempElement = elt.addElement("file");
tempElement.addAttribute("name", image.getName());
tempElement.addAttribute("thumb", image.getThumb());
tempElement.addAttribute("file", image.getFileurl());
tempElement.addCDATA(image.getDescription());
}
}
this.saveDocument();
}
}
上面用的的entity你就自己去建吧,至于从数据库里查数据你自己去写,没说什么说的
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.hibut.entity.Category;
import com.hibut.entity.ImageFile;
public class XMLEditor {
private File data;
private org.dom4j.Document doc;
public XMLEditor(String path) {
data = new File(path + "\\data.xml");
this.checkFile();
}
private void checkFile() {
if (!data.exists()) {
doc = DocumentHelper.createDocument();
doc.addComment("xml created at:" + new Date());
Element root = doc.addElement("data");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer;
try {
writer = new XMLWriter(new FileOutputStream(data), format);
writer.write(doc);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.getFile();
}
private void getFile() {
SAXReader reader = new SAXReader();
try {
doc = (Document) reader.read(data);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void saveDocument() {
XMLWriter writer = null;
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
try {
writer = new XMLWriter(new FileOutputStream(data), format);
writer.write(doc);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addImage(Category category) {
Element elt = null;
List list = doc.selectNodes("data/category[@name='" + category.getName() + "']");
if (list.size() > 0) {
elt = (Element) list.get(0);
Iterator iterator = category.getFiles().iterator();
ImageFile image = null;
while (iterator.hasNext()) {
image = (ImageFile) iterator.next();
Element tempElement = elt.addElement("file");
tempElement.addAttribute("name", image.getName());
tempElement.addAttribute("thumb", image.getThumb());
tempElement.addAttribute("file", image.getFileurl());
tempElement.addCDATA(image.getDescription());
}
this.saveDocument();
return;
}
// 添加的图片的图片分类不存在
elt = doc.getRootElement().addElement("category");// 创建分类
elt.addAttribute("name", category.getName());
elt.addAttribute("thumb", category.getThumb());
elt.addComment("category created at " + (new Date()).toString());
// 添加分类中的图片
if (category.getFiles().size() > 0) {
Iterator ite = category.getFiles().iterator();
while (ite.hasNext()) {
ImageFile image = (ImageFile) ite.next();
Element tempElement = elt.addElement("file");
tempElement.addAttribute("name", image.getName());
tempElement.addAttribute("thumb", image.getThumb());
tempElement.addAttribute("file", image.getFileurl());
tempElement.addCDATA(image.getDescription());
}
}
this.saveDocument();
}
}
上面用的的entity你就自己去建吧,至于从数据库里查数据你自己去写,没说什么说的
更多追问追答
追问
可以提供一下注解吗
追答
这个吧,我真没写注释了(觉得没必要),你哪看不懂问吧,我直接给你回答得了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |