package jdom;
/*
新闻专用XML类文件
功能:
删除由上到下
添加在最下面添加
读取由下向上
可以跟据ID删除记录
可以由上到下删除X条记录
添加时可以限制记录多少,多于时删除旧的再插入新数据
全程数据交换使用ArrayList 方便处理
不满意:
扩展性不强针对特定的XML文件,没能无限级访问子节点,性能还没得优化!
*/
import java.io.*;
import java.util.*;
import java.lang.*;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import com.ntsky.file.FileUtil;//一个文件上传的类~我调用来生成目录,有需要的请联系我..
public class WriteXML {
//初始化对象变量
private String root_jsp = new String(); //目录根路径
private String FileName = new String();//XML文件名
private String Secon_root = new String();//创建目录
private ArrayList PostList = null;//存放数据记录
private Map map = new HashMap();
private int MaxLine = 0;//XML节点的最大个数
private int docLine = 0;//存放XML个数
private Document read_doc = new Document();
private Document public_doc = new Document();
private String root_Temp_File = "";
private String first_dir = "";
public void BuildXML() throws Exception//新建一个XML文件
{
Element root,news_rom,id,title,File_route,postDay;
root = new Element("news_info"); //生成根元素:student-info
read_doc = new Document(root);
this.docLine = 0;
Build(root);
}
public void AddXML() throws Exception//向XML添加一个结点
{
Element root;
//生成根元素:info
root = read_doc.getRootElement(); //得到根元素 //将根元素植入文档doc中
Build(root);
}
public void Build(Element root) throws Exception//向XML文件写入数据
{
Element news_rom,id,title,File_route,postDay,sate;
if(docLine > MaxLine){
if(!DelXML(docLine - MaxLine)){//清理出空间
return;//如果清理出错退出程序
}else{this.docLine = MaxLine;}
}
int a = PostList.size() / 5;//要存放的记录个数
int b = MaxLine - docLine;//空位
if(a >= MaxLine)//如果需要添加的记录多于最大数目
a = MaxLine;//存放记录至于最大数目
if(a < 0)
a = PostList.size() / 5;
if(a - b >= 0){//存放记录的所需要的空间
if(!DelXML(a-b))//清理出空间
return;//如果清理出错退出程序
}
// for(int i = 0; i < (a * 5); i = i + 5)
for(int i = (a * 5)-1; i >= 0; i = i - 5)
{
news_rom = new Element("news_rom"); //生成元素:该元素中将包含元素number,name,age
id = new Element("id");
title = new Element("title");
File_route = new Element("File_route");
postDay = new Element("postDay");
sate = new Element("sate");
id.setText((String)PostList.get(i-4));
title.setText((String)PostList.get(i-3));
File_route.setText((String)PostList.get(i-2));
postDay.setText((String)PostList.get(i-1));
sate.setText((String)PostList.get(i));
news_rom.addContent(id);
news_rom.addContent(title);
news_rom.addContent(File_route);
news_rom.addContent(postDay);
news_rom.addContent(sate);
root.addContent(news_rom);
}
this.public_doc = read_doc;
}
public int Get_docLine()
{
return this.docLine;
}
public int Get_MaxLine()
{
return this.MaxLine;
}
public void ReadXml_defail(String province) throws Exception//读取记录的ID的节点记录
{
Element MyElement = read_doc.getRootElement(); //获得根节点
List listTwo = MyElement.getChildren();//得到list,准备遍历所有节点
Map a = new HashMap();
for(int i = 0;i < listTwo.size();i++)
{
Element e_key = (Element)listTwo.get(i);//得到节点下的元素
if(province.equals(e_key.getChild("province").getTextTrim()))//比较id值是否相等
{
Element e_key_child = e_key.getChild("url_xml");
for(int j = 1; j <= 9; j++)
a.put("New_"+j,e_key_child.getChildText("url_xml_"+j));
break;
}
}
this.map = a;
}
public ArrayList ReadXml_one(String id) throws Exception//读取记录的ID的节点记录
{
Element MyElement = read_doc.getRootElement(); //获得根节点
List listTwo = MyElement.getChildren();//得到list,准备遍历所有节点
ArrayList a = new ArrayList();
for(int i = 0;i < listTwo.size();i++)
{
Element e_key = (Element)listTwo.get(i);//得到节点下的元素
if(id.equals(e_key.getChild("id").getTextTrim()))//比较id值是否相等
{
a.add(e_key.getChild("title").getTextTrim());
a.add(e_key.getChild("File_route").getTextTrim());
a.add(e_key.getChild("postDay").getTextTrim());
a.add(e_key.getChild("sate").getTextTrim());
break;
}
}
return a;
}
public ArrayList ReadXml_All(int number) throws Exception//读取记录的所有记录
{
Element MyElement = read_doc.getRootElement(); //获得根节点
List listTwo = MyElement.getChildren();//得到list,准备遍历所有节点
ArrayList a = new ArrayList();
int temp = 0;;
if(listTwo.size() >= number)
temp = listTwo.size() - number;
else
temp = 0;
for(int i = listTwo.size() - 1;i >= 0 && i >= temp; i--)
{
Element e_key = (Element)listTwo.get(i);//得到节点下的元素
a.add(e_key.getChild("id").getTextTrim());
a.add(e_key.getChild("title").getTextTrim());
a.add(e_key.getChild("File_route").getTextTrim());
a.add(e_key.getChild("postDay").getTextTrim());
a.add(e_key.getChild("sate").getTextTrim());
}
return a;
}
public ArrayList ReadXml_All_id(int number) throws Exception//读取记录的所有记录
{
Element MyElement = read_doc.getRootElement(); //获得根节点
List listTwo = MyElement.getChildren();//得到list,准备遍历所有节点
ArrayList a = new ArrayList();
int temp = 0;;
if(listTwo.size() >= number)
temp = listTwo.size() - number;
else
temp = 0;
for(int i = listTwo.size() - 1;i >= 0 && i >= temp; i--)
{
Element e_key = (Element)listTwo.get(i);//得到节点下的元素
a.add(e_key.getChild("id").getTextTrim());
}
return a;
}
public void EditXml(String id) throws Exception//修改记录是ID的节点
{
Element MyElement = read_doc.getRootElement(); //获得根节点
List listTwo = MyElement.getChildren();//得到list,准备遍历所有节点
for(int i = 0;i < listTwo.size();i++)
{
Element e_key = (Element)listTwo.get(i);//得到节点下的元素
if(id.equals(e_key.getChild("id").getTextTrim()))//比较id值是否相等
{
Element title = e_key.getChild("title");
title.setText((String)PostList.get(1));
Element File_route = e_key.getChild("File_route");
File_route.setText((String)PostList.get(2));
Element postDay = e_key.getChild("postDay");
postDay.setText((String)PostList.get(3));
Element sate = e_key.getChild("sate");
sate.setText((String)PostList.get(4));
this.public_doc = read_doc;
break;
}
}
}
public boolean DelXML(String id) throws Exception//跟据ID修改指定的记录
{
boolean tmpBool = false;
Element MyElement = read_doc.getRootElement(); //获得根节点
List listTwo = MyElement.getChildren();//得到list,准备遍历所有节点
for(int i = 0;i < listTwo.size();i++)
{
Element e_key = (Element)listTwo.get(i);//得到节点下的元素
if(id.equals(e_key.getChild("id").getTextTrim()))//比较id值是否相等
{
e_key.removeChildren("news_rom");
MyElement.removeContent(e_key);
tmpBool = true;
break;
}
}
this.public_doc = read_doc;
return tmpBool;
}
public boolean DelXML(int number) throws Exception//删除XML中数据最后的number条记录
{
boolean tmpBool = false;
Element MyElement = read_doc.getRootElement(); //获得根节点
List listTwo = MyElement.getChildren();//得到list,准备遍历所有节点
for(int i = 0; i < number ;i++)
{
Element e_key = (Element)listTwo.get(0);//得到节点下的元素
e_key.removeChildren("news_rom");
MyElement.removeContent(e_key);
tmpBool = true;
}
this.public_doc = read_doc;
return tmpBool;
}
public void WreitXml_FIle() throws Exception//生成文件到硬盘
{
Format format = Format.getCompactFormat();
format.setEncoding("gb2312"); //设置xml文件的字符为gb2312
format.setIndent(" "); //设置xml文件的缩进为4个空格
XMLOutputter XMLOut = new XMLOutputter(format);//在元素后换行,每一层元素缩排四格
XMLOut.output(public_doc, new FileOutputStream(Get_FullRootFile()));
}
//从硬盘中取数数据到内存
public boolean readXML() throws Exception
{
try{
SAXBuilder builder = new SAXBuilder();
this.read_doc = builder.build(Get_FullRootFile());
Element MyElement = read_doc.getRootElement(); //获得根节点
List listTwo = MyElement.getChildren();
this.docLine = listTwo.size();
listTwo = null;
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public void Set_PostList(ArrayList PostList)
{//XML添加记录的数据源
this.PostList = PostList;
}
public void initialize(String url)
{//取servlet的根目录路径
this.root_jsp = url;
}
public void initialize(PageContext pageContext)
{//取jsp根目录路径
this.root_jsp = pageContext.getServletContext().getRealPath("/");
}
public void Set_MaxLint(int max)
{//最大许可存放记录
this.MaxLine = max;
}
public void SetXmlFileName(String FileName)
{//设置XML文件名
String temp = new String();
if(FileName.toLowerCase().endsWith(".xml")){
this.FileName = FileName;
}else{
temp = FileName + ".xml";
this.FileName = temp;
}
}
public void firstdir(String rot)
{//设置首目录
if(rot.endsWith("/"))
this.first_dir = rot;
else
this.first_dir = rot + "/";
}
public void Creatdir(){
try{
FileUtil c = new FileUtil();
String Fild_D = Get_Secon_root();
c.makeDir(Fild_D,root_jsp);
}catch(Exception e){
}
}
public void SetSecon_root(String Secon_root)
{//设置目录
if(Secon_root.endsWith("/"))
this.Secon_root = first_dir + Secon_root;
else
this.Secon_root = first_dir + Secon_root + "/";
}
public void SetFull_R_name(String roo){
//由上级程序输入数据
this.root_Temp_File = roo;
}
//返回数据
public String Get_initialize()
{//返回根路径
return this.root_jsp;
}
public String Get_SetXmlFileName()
{//返回XML文件名
return this.FileName;
}
public String Get_Secon_root()
{//返回创键目录
return this.Secon_root;
}
public String Get_FullRootFile()
{//返回详细的文件名路径
if(this.root_Temp_File.equals(""))
return Get_initialize() + Get_Secon_root() + Get_SetXmlFileName();
else
return Get_initialize() + this.root_Temp_File;
}
public String Get_dir_file(){
//返回详细的文件名路径
if(this.root_Temp_File.equals(""))
return Get_Secon_root() + Get_SetXmlFileName();
else
return this.root_Temp_File;
}
public Map getInputMap() {
return this.map;
}
public String getString(String key) {
// TODO 自动生成方法存根
return (String)map.get(key);
}
}
用于管理两个XML文件第一个(这个XML存放第二个XML的路径):
<?xml version="1.0" encoding="gb2312"?>
<root>
<date>
<province>defail</province>
<url_xml>
<url_xml_1>xml/x02.xml</url_xml_1>
<url_xml_2>xml/x02.xml</url_xml_2>
<url_xml_3>nofile</url_xml_3>
<url_xml_4>nofile</url_xml_4>
<url_xml_5>xml/x05.xml</url_xml_5>
<url_xml_6>nofile</url_xml_6>
<url_xml_7>xml/x07.xml</url_xml_7>
<url_xml_8>xml/x08.xml</url_xml_8>
<url_xml_9>nofile</url_xml_9>
</url_xml>
</date>
<date>
<province>1</province>
<url_xml>
<url_xml_1>nofile</url_xml_1>
<url_xml_2>xml/1/x02.xml</url_xml_2>
<url_xml_3>nofile</url_xml_3>
<url_xml_4>nofile</url_xml_4>
<url_xml_5>xml/1/x05.xml</url_xml_5>
<url_xml_6>nofile</url_xml_6>
<url_xml_7>nofile</url_xml_7>
<url_xml_8>nofile</url_xml_8>
<url_xml_9>nofile</url_xml_9>
</url_xml>
</date>
</root>
第二个(存放一些新闻基本信息):
<?xml version="1.0" encoding="gb2312"?>
<news_info>
<news_rom>
<id>22</id>
<title>title</title>
<File_route>File.htm</File_route>
<postDay>2005-01-25 23:51:52.0</postDay>
<sate>3</sate>
</news_rom>
</news_info>