XmlOperationTool.java
package com.cemso.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import com.cemso.dto.OnebyoneDTO;
import com.cemso.dto.PlaylistDTO;
import com.cemso.dto.ProgramDTO;
import com.cemso.dto.ResourceDTO;
import com.cemso.dto.TemplateDTO;
import com.cemso.dto.TemplateModelDTO;
public class XmlOperationTool {
private static final String RESOURCEXMLPATH = "c:/cross/CPSolution/XmlDB/ResourceDB.xml";
private static final String TEMPLATEXMLPATH = "c:/cross/CPSolution/XmlDB/TemplateDB.xml";
private static final String PROGRAMXMLPATH = "c:/cross/CPSolution/XmlDB/ProgramDB.xml";
private static final String PLAYLISTXMLPATH = "c:/cross/CPSolution/XmlDB/PlaylistDB.xml";
public static final String DB_TYPE_RESOURCE = "resource";
public static final String DB_TYPE_TEMPLATE = "template";
public static final String DB_TYPE_PROGRAM = "program";
public static final String DB_TYPE_PLAYLIST = "playlist";
// 查询所有的数据
public static List<?> list(String dbType) throws JDOMException, IOException {
List<ResourceDTO> resourceList = null;
List<TemplateDTO> templateList = null;
List<ProgramDTO> programList = null;
List<PlaylistDTO> playlistList = null;
SAXBuilder builder = new SAXBuilder();
String xmlPath = null;
if ("resource".equals(dbType)) {
xmlPath = RESOURCEXMLPATH;
resourceList = new ArrayList<ResourceDTO>();
// 获得文档对象
Document document = builder.build(xmlPath);
// 获得根节点
Element root = document.getRootElement();
List<?> list = root.getChildren();
Iterator<?> it = list.iterator();
while (it.hasNext()) {
Element e = (Element) it.next();
ResourceDTO resource = new ResourceDTO();
resource.setId(e.getAttributeValue("id"));
resource.setResourceName(e.getAttributeValue("name"));
resource.setResourceType(e.getAttributeValue("type"));
Iterator<?> eIt = e.getChildren().iterator();
while (eIt.hasNext()) {
Element ec = (Element) eIt.next();
if (ec.getAttributeValue("name").equals("remark")) {
resource.setParamRemark(ec.getText());
}
if (ec.getAttributeValue("name").equals("createtime")) {
resource.setParamCreatetime(ec.getText());
}
if (ec.getAttributeValue("name").equals("createby")) {
resource.setParamCreateby(ec.getText());
}
}
resourceList.add(resource);
}
}
if ("template".equals(dbType)) {
xmlPath = TEMPLATEXMLPATH;
templateList = new ArrayList<TemplateDTO>();
// 获得文档对象
Document document = builder.build(xmlPath);
// 获得根节点
Element root = document.getRootElement();
List<?> list = root.getChildren();
Iterator<?> it = list.iterator();
while (it.hasNext()) {
Element e = (Element) it.next();
TemplateDTO template = new TemplateDTO();
template.setId(e.getAttributeValue("id"));
template.setName(e.getAttributeValue("name"));
Iterator<?> eIt = e.getChildren().iterator();
while (eIt.hasNext()) {
Element ec = (Element) eIt.next();
if (ec.getAttributeValue("name").equals("remark")) {
template.setRemark(ec.getText());
}
if (ec.getAttributeValue("name").equals("createtime")) {
template.setCreateTime(ec.getText());
}
if (ec.getAttributeValue("name").equals("createby")) {
template.setCreateBy(ec.getText());
}
if (ec.getAttributeValue("name").equals("models")) {
Iterator<?> itModels = ec.getChildren().iterator();
List<TemplateModelDTO> models = new ArrayList<TemplateModelDTO>();
while (itModels.hasNext()) {
TemplateModelDTO modelDTO = new TemplateModelDTO();
Element model = (Element) itModels.next();
modelDTO.setId(model.getAttributeValue("id"));
modelDTO.setName(model.getAttributeValue("name"));
modelDTO.setType(model.getAttributeValue("type"));
modelDTO.setGradation(model
.getAttributeValue("gradation"));
modelDTO.setDimension(model
.getAttributeValue("dimension"));
models.add(modelDTO);
}
template.setModels(models);
}
}
templateList.add(template);
}
}
// program list
if("program".equals(dbType)){
xmlPath = PROGRAMXMLPATH;
programList = new ArrayList<ProgramDTO>();
// 获得文档对象
Document document = builder.build(xmlPath);
// 获得根节点
Element root = document.getRootElement();
List<?> list = root.getChildren();
Iterator<?> it = list.iterator();
while (it.hasNext()) {
Element e = (Element) it.next();
ProgramDTO program = new ProgramDTO();
program.setId(e.getAttributeValue("id"));
program.setName(e.getAttributeValue("name"));
Iterator<?> eIt = e.getChildren().iterator();
while (eIt.hasNext()) {
Element ec = (Element) eIt.next();
if (ec.getAttributeValue("name").equals("remark")) {
program.setRemark(ec.getText());
}
if (ec.getAttributeValue("name").equals("createtime")) {
program.setCreatetime(ec.getText());
}
if (ec.getAttributeValue("name").equals("createby")) {
program.setCreateby(ec.getText());
}
if (ec.getAttributeValue("name").equals("length")) {
program.setLength(ec.getText());
}
if (ec.getAttributeValue("name").equals("template")) {
program.setTemplateId(ec.getText());
}
if (ec.getAttributeValue("name").equals("onebyones")) {
Iterator<?> itOnebyone = ec.getChildren().iterator();
List<OnebyoneDTO> onebyones = new ArrayList<OnebyoneDTO>();
while (itOnebyone.hasNext()) {
OnebyoneDTO onebyoneDTO = new OnebyoneDTO();
Element onebyone = (Element) itOnebyone.next();
onebyoneDTO.setDimension(onebyone.getAttributeValue("dimension"));
onebyoneDTO.setResource(onebyone.getAttributeValue("resource"));
onebyoneDTO.setGradation(onebyone.getAttributeValue("gradation"));
onebyones.add(onebyoneDTO);
}
program.setOnebyoneList(onebyones);
}
}
programList.add(program);
}
}
// playlist list
if("playlist".equals(dbType)){
xmlPath = PLAYLISTXMLPATH;
playlistList = new ArrayList<PlaylistDTO>();
// 获得文档对象
Document document = builder.build(xmlPath);
// 获得根节点
Element root = document.getRootElement();
List<?> list = root.getChildren();
Iterator<?> it = list.iterator();
while (it.hasNext()) {
Element e = (Element) it.next();
PlaylistDTO playlist = new PlaylistDTO();
playlist.setId(e.getAttributeValue("id"));
playlist.setName(e.getAttributeValue("name"));
Iterator<?> eIt = e.getChildren().iterator();
while (eIt.hasNext()) {
Element ec = (Element) eIt.next();
if (ec.getAttributeValue("name").equals("remark")) {
playlist.setRemark(ec.getText());
}
if (ec.getAttributeValue("name").equals("createtime")) {
playlist.setCreatetime(ec.getText());
}
if (ec.getAttributeValue("name").equals("createby")) {
playlist.setCreateby(ec.getText());
}
if (ec.getAttributeValue("name").equals("startnow")) {
playlist.setStartnow(Boolean.valueOf(ec.getText()));
}
if (ec.getAttributeValue("name").equals("starttime")) {
playlist.setStarttime(ec.getText());
}
if (ec.getAttributeValue("name").equals("endtime")) {
playlist.setEndtime(ec.getText());
}
if (ec.getAttributeValue("name").equals("programs")) {
Iterator<?> itt = ec.getChildren().iterator();
List<ProgramDTO> programs = new ArrayList<ProgramDTO>();
while (itt.hasNext()) {
ProgramDTO programDTO = new ProgramDTO();
Element program = (Element) itt.next();
programDTO.setId(program.getAttributeValue("id"));
programDTO.setIndex(program.getAttributeValue("index"));
programs.add(programDTO);
}
playlist.setProgramList(programs);
}
}
playlistList.add(playlist);
}
}
if("resource".equals(dbType)){
return resourceList;
}
if("template".equals(dbType)){
return templateList;
}
if("program".equals(dbType)){
return programList;
}
if("playlist".equals(dbType)){
return playlistList;
}
return null;
}
// 在原有的ResourceDB.xml文件中添加数据
public static void add(ResourceDTO resource) throws JDOMException,
FileNotFoundException, IOException {
SAXBuilder builder = new SAXBuilder();
String xmlPath = RESOURCEXMLPATH;
Document document = builder.build(xmlPath);
// 获得根节点
Element root = document.getRootElement();
// 创建节点resource
Element element = new Element("resource");
// 给resource节点添加属性id,name,type
element.setAttribute("id", resource.getId());
element.setAttribute("name", resource.getResourceName());
element.setAttribute("type", resource.getResourceType());
// 给resource节点添加子节点并赋值
Element param1 = new Element("param");
param1.setAttribute("name", "remark");
param1.setText(resource.getParamRemark());
Element param2 = new Element("param");
param2.setAttribute("name", "createtime");
param2.setText(resource.getParamCreatetime());
Element param3 = new Element("param");
param3.setAttribute("name", "createby");
param3.setText(resource.getParamCreateby());
element.addContent(param1);
element.addContent(param2);
element.addContent(param3);
// 给父节点添加person子节点
root.addContent(element);
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
output.output(document, new FileOutputStream(xmlPath));
}
// 在原有的ProgramDB.xml文件中添加数据
public static void add(TemplateDTO template) throws JDOMException,
FileNotFoundException, IOException {
SAXBuilder builder = new SAXBuilder();
String xmlPath = TEMPLATEXMLPATH;
Document document = builder.build(xmlPath);
// 获得根节点
Element root = document.getRootElement();
// 创建节点template
Element element = new Element("template");
// 给template节点添加属性id,name
element.setAttribute("id", template.getId());
element.setAttribute("name", template.getName());
// 给template节点添加子节点并赋值
Element param1 = new Element("param");
param1.setAttribute("name", "remark");
param1.setText(template.getRemark());
Element param2 = new Element("param");
param2.setAttribute("name", "createtime");
param2.setText(template.getCreateTime());
Element param3 = new Element("param");
param3.setAttribute("name", "createby");
param3.setText(template.getCreateBy());
Element param4 = new Element("param");
param4.setAttribute("name", "models");
// 添加model
for (TemplateModelDTO model : template.getModels()) {
Element paramChild = new Element("model");
paramChild.setAttribute("id", model.getId());
paramChild.setAttribute("name", model.getName());
paramChild.setAttribute("type", model.getType());
paramChild.setAttribute("dimension", model.getDimension());
paramChild.setAttribute("gradation", model.getGradation());
param4.addContent(paramChild);
}
element.addContent(param1);
element.addContent(param2);
element.addContent(param3);
element.addContent(param4);
// 给父节点添加person子节点
root.addContent(element);
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
output.output(document, new FileOutputStream(xmlPath));
}
// 在原有的TemplateDB.xml文件中添加数据
public static void add(ProgramDTO program) throws JDOMException,
FileNotFoundException, IOException {
SAXBuilder builder = new SAXBuilder();
String xmlPath = PROGRAMXMLPATH;
Document document = builder.build(xmlPath);
// 获得根节点
Element root = document.getRootElement();
// 创建节点program
Element element = new Element("program");
// 给program节点添加属性id,name
element.setAttribute("id", program.getId());
element.setAttribute("name", program.getName());
// 给program节点添加子节点并赋值
Element param1 = new Element("param");
param1.setAttribute("name", "remark");
param1.setText(program.getRemark());
Element param2 = new Element("param");
param2.setAttribute("name", "createtime");
param2.setText(program.getCreatetime());
Element param3 = new Element("param");
param3.setAttribute("name", "createby");
param3.setText(program.getCreateby());
Element param4 = new Element("param");
param4.setAttribute("name", "length");
param4.setText(program.getLength());
Element param5 = new Element("param");
param5.setAttribute("name", "template");
param5.setText(program.getTemplateId());
Element param6 = new Element("param");
param6.setAttribute("name", "onebyones");
// 添加onebyone
for (OnebyoneDTO onebyone : program.getOnebyoneList()) {
Element paramChild = new Element("onebyone");
paramChild.setAttribute("resource", onebyone.getResource());
paramChild.setAttribute("dimension", onebyone.getDimension());
paramChild.setAttribute("gradation", onebyone.getGradation());
param6.addContent(paramChild);
}
element.addContent(param1);
element.addContent(param2);
element.addContent(param3);
element.addContent(param4);
element.addContent(param5);
element.addContent(param6);
// 给父节点添加person子节点
root.addContent(element);
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
output.output(document, new FileOutputStream(xmlPath));
}
// 在原有的PlaylistDB.xml文件中添加数据
public static void add(PlaylistDTO playlist) throws JDOMException,
FileNotFoundException, IOException {
SAXBuilder builder = new SAXBuilder();
String xmlPath = PLAYLISTXMLPATH;
Document document = builder.build(xmlPath);
// 获得根节点
Element root = document.getRootElement();
// 创建节点program
Element element = new Element("playlist");
// 给program节点添加属性id,name
element.setAttribute("id", playlist.getId());
element.setAttribute("name", playlist.getName());
// 给program节点添加子节点并赋值
Element param1 = new Element("param");
param1.setAttribute("name", "remark");
param1.setText(playlist.getRemark());
Element param2 = new Element("param");
param2.setAttribute("name", "createtime");
param2.setText(playlist.getCreatetime());
Element param3 = new Element("param");
param3.setAttribute("name", "createby");
param3.setText(playlist.getCreateby());
Element param4 = new Element("param");
param4.setAttribute("name", "startnow");
param4.setText(String.valueOf(playlist.isStartnow()));
Element param5 = new Element("param");
param5.setAttribute("name", "starttime");
param5.setText(playlist.getStarttime());
Element param6 = new Element("param");
param6.setAttribute("name", "endtime");
param6.setText(playlist.getEndtime());
Element param7 = new Element("param");
param7.setAttribute("name", "programs");
// 添加programs
int size = playlist.getProgramList().size();
for (int i = 0; i < size; i++) {
ProgramDTO program = playlist.getProgramList().get(i);
Element paramChild = new Element("program");
paramChild.setAttribute("id", program.getId());
paramChild.setAttribute("index", String.valueOf(i));
param7.addContent(paramChild);
}
element.addContent(param1);
element.addContent(param2);
element.addContent(param3);
element.addContent(param4);
element.addContent(param5);
element.addContent(param6);
element.addContent(param7);
// 给父节点添加person子节点
root.addContent(element);
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
output.output(document, new FileOutputStream(xmlPath));
}
// 修改数据
public static void edit(Object object) throws JDOMException,
FileNotFoundException, IOException {
SAXBuilder builder = new SAXBuilder();
String xmlPath = "";
// resource object
if(object instanceof ResourceDTO){
xmlPath = RESOURCEXMLPATH;
ResourceDTO resource = (ResourceDTO)object;
Document document = builder.build(xmlPath);
Element root = document.getRootElement();
List<?> list = root.getChildren();
Iterator<?> it = list.iterator();
for (int i = 0; i < list.size(); i++) {
Element e = (Element) it.next();
if (e.getAttributeValue("id").equals(resource.getId())) {
List<?> paramList = e.getChildren();
for(int j = 0; j < paramList.size(); j++){
Element ee = (Element)paramList.get(j);
if(ee.getAttributeValue("name").equals("remark")){
ee.setText(resource.getParamRemark());
}
if(ee.getAttributeValue("name").equals("createtime")){
ee.setText(resource.getParamCreatetime());
}
if(ee.getAttributeValue("name").equals("createby")){
ee.setText(resource.getParamCreateby());
}
}
}
}
XMLOutputter output = new XMLOutputter();
output.output(document, new FileOutputStream(xmlPath));
}
// template object
if(object instanceof TemplateDTO){
xmlPath = TEMPLATEXMLPATH;
TemplateDTO template = (TemplateDTO)object;
Document document = builder.build(xmlPath);
Element root = document.getRootElement();
List<?> list = root.getChildren();
Iterator<?> it = list.iterator();
for (int i = 0; i < list.size(); i++) {
Element e = (Element) it.next();
if (e.getAttributeValue("id").equals(template.getId())) {
List<?> paramList = e.getChildren();
for(int j = 0; j < paramList.size(); j++){
Element ee = (Element)paramList.get(j);
if(ee.getAttributeValue("name").equals("remark")){
ee.setText(template.getRemark());
}
if(ee.getAttributeValue("name").equals("createtime")){
ee.setText(template.getCreateTime());
}
if(ee.getAttributeValue("name").equals("createby")){
ee.setText(template.getCreateBy());
}
// models update
List<?> eee = ee.getChildren();
/**
*
*
*
*
* models update here.
*
*
*
*
*
*/
}
}
}
XMLOutputter output = new XMLOutputter();
output.output(document, new FileOutputStream(xmlPath));
}
// program object
if(object instanceof ProgramDTO){
xmlPath = PROGRAMXMLPATH;
}
}
// 删除
public static void del(String dbType, String elementId) throws JDOMException, FileNotFoundException,
IOException {
SAXBuilder builder = new SAXBuilder();
String xmlPath = "";
// delete resource element
if(dbType.equals("resource")){
xmlPath = RESOURCEXMLPATH;
}
if(dbType.equals("template")){
xmlPath = TEMPLATEXMLPATH;
}
if(dbType.equals("program")){
xmlPath = PROGRAMXMLPATH;
}
Document document = builder.build(xmlPath);
Element root = document.getRootElement();
List<?> list = root.getChildren();
Iterator<?> it = list.iterator();
for (int i = 0; i < list.size(); i++) {
Element e = (Element) it.next();
if (elementId.equals(e.getAttributeValue("id"))) {
root.removeContent(e);
break;
}
}
XMLOutputter out = new XMLOutputter();
out.output(document, new FileOutputStream(xmlPath));
}
public static void main(String[] args) throws FileNotFoundException, JDOMException, IOException {
del("template","template_1329281108962");
System.out.println(RESOURCEXMLPATH);
}
}
PlaylistDTO.java
/*
* Created on Feb 17, 2012
*
* PlaylistDTO.java
*
* Copyright (C) 2012 by Citicorp Software & Technology Services (Shanghai) Limited.
* All rights reserved. Citicorp Software & Technology Services (Shanghai) Limited
* claims copyright in this computer program as an unpublished work. Claim of copyright
* does not imply waiver of other rights.
*
* NOTICE OF PROPRIETARY RIGHTS
*
* This program is a confidential trade secret and the property of
* Citicorp Software & Technology Services (Shanghai) Limited. Use, examination,
* reproduction, disassembly, decompiling, transfer and/or disclosure to others of
* all or any part of this software program are strictly prohibited except by express
* written agreement with Citicorp Software & Technology Services (Shanghai) Limited.
*/
/*
* ---------------------------------------------------------------------------------
* Modification History
* Date Author Version Description
* Feb 17, 2012 gl65293 1.0 New
* ---------------------------------------------------------------------------------
*/
/**
*
*/
package com.cemso.dto;
import java.util.List;
/**
* @author gl65293
*
*/
public class PlaylistDTO {
private String id;
private String name;
private String remark;
private String createtime;
private String createby;
private boolean startnow;
private String starttime;
private String endtime;
private List<ProgramDTO> programList;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getCreateby() {
return createby;
}
public void setCreateby(String createby) {
this.createby = createby;
}
public boolean isStartnow() {
return startnow;
}
public void setStartnow(boolean startnow) {
this.startnow = startnow;
}
public String getStarttime() {
return starttime;
}
public void setStarttime(String starttime) {
this.starttime = starttime;
}
public String getEndtime() {
return endtime;
}
public void setEndtime(String endtime) {
this.endtime = endtime;
}
public List<ProgramDTO> getProgramList() {
return programList;
}
public void setProgramList(List<ProgramDTO> programList) {
this.programList = programList;
}
}
PlaylistService.java
/* * Created on Feb 17, 2012 * * PlaylistService.java * * Copyright (C) 2012 by Citicorp Software & Technology Services (Shanghai) Limited. * All rights reserved. Citicorp Software & Technology Services (Shanghai) Limited * claims copyright in this computer program as an unpublished work. Claim of copyright * does not imply waiver of other rights. * * NOTICE OF PROPRIETARY RIGHTS * * This program is a confidential trade secret and the property of * Citicorp Software & Technology Services (Shanghai) Limited. Use, examination, * reproduction, disassembly, decompiling, transfer and/or disclosure to others of * all or any part of this software program are strictly prohibited except by express * written agreement with Citicorp Software & Technology Services (Shanghai) Limited. */ /* * --------------------------------------------------------------------------------- * Modification History * Date Author Version Description * Feb 17, 2012 gl65293 1.0 New * --------------------------------------------------------------------------------- */ /** * */ package com.cemso.service; import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; import org.jdom.JDOMException; import com.cemso.dto.PlaylistDTO; /** * @author gl65293 * */ public interface PlaylistService { public boolean addPlaylist(PlaylistDTO playlist) throws FileNotFoundException, JDOMException, IOException; public List<PlaylistDTO> getPlaylist() throws JDOMException, IOException; }
PlaylistServiceImpl.java/*
* Created on Feb 17, 2012
*
* PlaylistServiceImpl.java
*
* Copyright (C) 2012 by Citicorp Software & Technology Services (Shanghai) Limited.
* All rights reserved. Citicorp Software & Technology Services (Shanghai) Limited
* claims copyright in this computer program as an unpublished work. Claim of copyright
* does not imply waiver of other rights.
*
* NOTICE OF PROPRIETARY RIGHTS
*
* This program is a confidential trade secret and the property of
* Citicorp Software & Technology Services (Shanghai) Limited. Use, examination,
* reproduction, disassembly, decompiling, transfer and/or disclosure to others of
* all or any part of this software program are strictly prohibited except by express
* written agreement with Citicorp Software & Technology Services (Shanghai) Limited.
*/
/*
* ---------------------------------------------------------------------------------
* Modification History
* Date Author Version Description
* Feb 17, 2012 gl65293 1.0 New
* ---------------------------------------------------------------------------------
*/
/**
*
*/
package com.cemso.service.impl;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.jdom.JDOMException;
import com.cemso.dto.PlaylistDTO;
import com.cemso.service.PlaylistService;
import com.cemso.util.XmlOperationTool;
/**
* @author gl65293
*
*/
public class PlaylistServiceImpl implements PlaylistService {
/* (non-Javadoc)
* @see com.cemso.service.PlaylistService#addPlaylist(com.cemso.dto.PlaylistDTO)
*/
public boolean addPlaylist(PlaylistDTO playlist) throws FileNotFoundException, JDOMException, IOException {
XmlOperationTool.add(playlist);
return true;
}
/* (non-Javadoc)
* @see com.cemso.service.PlaylistService#getPlaylist()
*/
@SuppressWarnings("unchecked")
public List<PlaylistDTO> getPlaylist() throws JDOMException, IOException {
List<PlaylistDTO> list = (List<PlaylistDTO>) XmlOperationTool.list(XmlOperationTool.DB_TYPE_PLAYLIST);
return list;
}
}
PlaylistDB.xml
<?xml version="1.0" encoding="UTF-8"?>
<playlists>
<playlist id="playlist_1329462432344" name="222">
<param name="remark">22</param>
<param name="createtime">2012-02-17 15:07:12</param>
<param name="createby"></param>
<param name="startnow">true</param>
<param name="starttime"></param>
<param name="endtime"></param>
<param name="programs">
<program id="" index="1" />
<program id="" index="1" />
<program id="" index="1" />
<program id="" index="1" />
</param>
</playlist>
</playlists>
243




被折叠的 条评论
为什么被折叠?



