package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class CreatEpg {
public static epg Xml(File file){
epg epg = new epg();
//设置当前时间
epg.setCreatedTime(getDate());
epg.setVersion("1.0");
epg.setChannelCode("0"+file.getName().substring(file.getName().indexOf(".")-1, file.getName().indexOf(".")) + "25");
epg.setChannelName(file.getName().substring(file.getName().indexOf("_")+1,file.getName().indexOf(".")));
epg.setTerminalType("stb,mobile");
epg.setDefinitionType("SD");
epg.setBusiType("");
//info 数据
info info = new info("","中国大陆","","普通话","","0.0","综合,对话,热点");
List<Program> programList = new ArrayList<Program>();
try {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String tempLine = "";
int line = 0;
while((tempLine = bufferedReader.readLine()) != null){
line ++ ;
//处理文件
Program program = new Program();
String[] lineContent = tempLine.split(
",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1);// 按逗号分开
program.setEndTime(lineContent[2]);
program.setStartTime(lineContent[1]);
name name = new name("chi",lineContent[0]);
program.setName(name);
String random = "";
for(int i = 0; i<3;i++){
Integer integer = new Random().nextInt(10);
random+=String.valueOf(integer);
}
String assertId = "mcap" + epg.getChannelCode() + random;
program.setAssetId(assertId);
program.setInfo(info);
program.setReserve("0");
program.setPosterList("");
program.setDescription("");
programList.add(program);
}
System.out.println(line);
epg.setProgramList(programList);
bufferedReader.close();
fileReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return epg;
}
public static void main(String[] args) {
try {
// File file1 = new File("F://output_cctv1.txt");
// File file2 = new File("F://output_cctv2.txt");
// File file3 = new File("F://output_cctv3.txt");
// File file4 = new File("F://output_cctv4.txt");
File file5 = new File("F://output_cctv5.txt");
File file6 = new File("F://output_cctv6.txt");
List<File> files = new ArrayList<File>();
// files.add(file6);files.add(file5);files.add(file4);files.add(file3);files.add(file2);files.add(file1);
files.add(file5);files.add(file6);
JAXBContext context = JAXBContext.newInstance(epg.class);
// 下面代码演示将对象转变为xml
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //设置XML自动换行
for(File file : files){
epg epg = CreatEpg.Xml(file);
//要存储的文件路径
File target_file = new File ("F:\\test\\");
if(!target_file.exists()){
target_file.mkdirs();
}
FileWriter fw = new FileWriter(target_file.getAbsolutePath()+"\\"+file.getName().substring(file.getName().indexOf("_")+1,file.getName().indexOf("."))+".xml");
m.marshal(epg, fw);
fw.close();
}
} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取当前时间
* */
public static String getDate(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
return dateFormat.format(date);
}
}
package test;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class epg {
private String createdTime;
private String version;
private String channelCode;
private String channelName;
private String terminalType;
private String definitionType;
private String busiType;
private List<Program> programList;
public epg() {
super();
// TODO Auto-generated constructor stub
}
public epg(String createdTime, String version, String channelCode,
String channelName, String terminalType, String definitionType,
String busiType, List<Program> programList) {
super();
this.createdTime = createdTime;
this.version = version;
this.channelCode = channelCode;
this.channelName = channelName;
this.terminalType = terminalType;
this.definitionType = definitionType;
this.busiType = busiType;
this.programList = programList;
}
@XmlAttribute
public String getCreatedTime() {
return createdTime;
}
public void setCreatedTime(String createdTime) {
this.createdTime = createdTime;
}
@XmlAttribute
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@XmlElement
public String getChannelCode() {
return channelCode;
}
public void setChannelCode(String channelCode) {
this.channelCode = channelCode;
}
@XmlElement
public String getChannelName() {
return channelName;
}
public void setChannelName(String channelName) {
this.channelName = channelName;
}
@XmlElement
public String getTerminalType() {
return terminalType;
}
public void setTerminalType(String terminalType) {
this.terminalType = terminalType;
}
@XmlElement
public String getDefinitionType() {
return definitionType;
}
public void setDefinitionType(String definitionType) {
this.definitionType = definitionType;
}
@XmlElement
public String getBusiType() {
return busiType;
}
public void setBusiType(String busiType) {
this.busiType = busiType;
}
@XmlElementWrapper
@XmlElement(name="program")
public List<Program> getProgramList() {
return programList;
}
public void setProgramList(List<Program> programList) {
this.programList = programList;
}
}
package test;
import javax.xml.bind.annotation.XmlAttribute;
public class info {
private String actors;
private String area;
private String director;
private String language;
private String playdate;
private String score;
private String type;
public info(String actors, String area, String director, String language,
String playdate, String score, String type) {
super();
this.actors = actors;
this.area = area;
this.director = director;
this.language = language;
this.playdate = playdate;
this.score = score;
this.type = type;
}
public info() {
super();
// TODO Auto-generated constructor stub
}
@XmlAttribute
public String getActors() {
return actors;
}
public void setActors(String actors) {
this.actors = actors;
}
@XmlAttribute
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
@XmlAttribute
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
@XmlAttribute
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
@XmlAttribute
public String getPlaydate() {
return playdate;
}
public void setPlaydate(String playdate) {
this.playdate = playdate;
}
@XmlAttribute
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
@XmlAttribute
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
package test;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
public class name {
private String language;
private String aaa;
@XmlAttribute
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
@XmlValue
public String getAaa() {
return aaa;
}
public void setAaa(String aaa) {
this.aaa = aaa;
}
public name() {
super();
}
public name(String language,String aa) {
super();
this.language = language;
this.aaa=aa;
}
}