1.创建xml文件
public static void createXml(File file,Source source){
try {
// 1、创建document对象
Document document = DocumentHelper.createDocument();
// 2、创建根节点rss
Element rss = document.addElement("playbill");
// 3、向rss节点添加version属性
// rss.addAttribute("billID", "20180928154502-1671404210");
// rss.addAttribute("billName", "222");
// rss.addAttribute("startTime", "2018-09-28 15:45:02");
// rss.addAttribute("storagetype", "isSHLive");
// 4、生成子节点及子节点内容
Element billIDele = rss.addElement("billID");
billIDele.setText(String.valueOf(source.getSourceID()));
Element billNameele = rss.addElement("billName");
billNameele.setText(source.getSourceName());
Element startTimeele = rss.addElement("startTime");
startTimeele.setText(source.getUploadedTime());
Element storagetypeele = rss.addElement("storagetype");
storagetypeele.setText("isSHLive");
Element Serialele = rss.addElement("Serial");
Element Parallelele = Serialele.addElement("Parallel");
// Element title = channel.addElement("title");
// title.setText("国内最新新闻");
// 5、设置生成xml的格式
// updateXml(new File("F:\\4343\\"),playinfo);
OutputFormat format = OutputFormat.createPrettyPrint();
// 设置编码格式
format.setEncoding("GBK");
// 6、生成xml文件
//File file = new File("C://Users//Administrator//Desktop//rss.xml");
XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
// 设置是否转义,默认使用转义字符
writer.setEscapeText(false);
writer.write(document);
writer.close();
System.out.println("生成rss.xml成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("生成rss.xml失败");
}
finally {
}
}
2.修改生成的xml文件(添加子标签)
//给xml文件增加节点以及内容
public static void updateXml(File file,Playinfo playinfo){
try {
SAXReader sr = new SAXReader();
sr.setEncoding("GBK");//这里设置文件编码
// Document document = reader.read(new BufferedReader(new InputStreamReader(new FileInputStream(new File(filename)),"UTF-8")));
Document document = sr.read(file);
Element root = document.getRootElement();
Element Serial = root.element("Serial");
Element rss = Serial.element("Parallel");
Element Mediaele = rss.addElement("Media");
Mediaele.addAttribute("host", "rtmp://__SERVICEIP__/vod/");
Mediaele.addAttribute("url", "mp4:"+playinfo.getMp4filenamequan());
Mediaele.addAttribute("type", "RTMP");
Element Mp4fileNameele = Mediaele.addElement("Mp4fileName");
Mp4fileNameele.setText(playinfo.getMp4filenamequan());
Element Widthele = Mediaele.addElement("Width");
Widthele.setText("1920");
Element Heightele = Mediaele.addElement("Height");
Heightele.setText("1080");
Element Mp4FileSizeele = Mediaele.addElement("Mp4FileSize");
Mp4FileSizeele.setText(playinfo.getMp4filesize());
Element durationele = Mediaele.addElement("duration");
durationele.setText("00:01:57");
Element Roleele = Mediaele.addElement("Role");
Roleele.setText(playinfo.getRole());
saveDocument(document, file);
System.out.println("修改xml成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("修改失败");
}
}
public static void saveDocument(Document document, File xmlFile) throws IOException {
Writer osWrite = new OutputStreamWriter(new FileOutputStream(xmlFile));// 创建输出流
OutputFormat format = OutputFormat.createPrettyPrint(); // 获取输出的指定格式
format.setEncoding("GBK");// 设置编码 ,确保解析的xml为UTF-8格式
XMLWriter writer = new XMLWriter(osWrite, format);// XMLWriter
// 设置是否转义,默认使用转义字符
writer.setEscapeText(false);
writer.write(document);
writer.flush();
writer.close();
}
3测试main方法
public static void main(String[] args) {
dom4jTest test = new dom4jTest();
//test.getDownplay("/WebRoot/2433185143-985549700_2_video.xml");
Source source = new Source();
source.setSourceID(3123);
source.setSourceName("长得");
source.setUploadedTime("2022");
File file = new File("C://Users//Administrator//Desktop//reurewoiru.xml");
test.createXml(file,source);
/*Playinfo playinfo = new Playinfo();
playinfo.setDuration("03:01:33");
playinfo.setHeight("1920");
playinfo.setWith("1080");
playinfo.setMp4filename("124");
playinfo.setMp4filenamequan("124.mp4");
playinfo.setMp4filesize("20M");
playinfo.setRole("3");
updateXml(new File("F:\\4343\\20191108141955-1180309147_0_video.xml"), playinfo);*/
}
4生成的xml文件
<?xml version="1.0" encoding="GBK"?>
<playbill>
<billID>16345</billID>
<billName>16345</billName>
<startTime>Tue May 26 16:06:34 CST 2020</startTime>
<storagetype>isSHLive</storagetype>
<Serial>
<Parallel>
<Media host="rtmp://__SERVICEIP__/vod/" url="mp4:124.mp4" type="RTMP">
<Mp4fileName>16345.mp4</Mp4fileName>
<Width>1920</Width>
<Height>1080</Height>
<Mp4FileSize>20M</Mp4FileSize>
<duration>00:01:57</duration>
<Role>3</Role>
</Media>
</Parallel>
</Serial>
</playbill>