/**
*使用Dom4j编写XML文档
*/
public void createXml(File file) {
// XML 声明 <?xmlversion=”1.0″ encoding=”UTF-8″?> 自动添加到 XML文档中
// 使用DocumentHelper类创建文档实例(生成 XML文档节点的 dom4jAPI工厂类)
Document document =DocumentHelper.createDocument();
// 使用addElement()方法创建根元素employees(用于向 XML 文档中增加元素)
Element root =document.addElement(“Busap”);
long sum =0;
for(long i= 0;i<1000;i++){
String time = getTime(sum);
if(!TextUtils.isEmpty(time)){
item(root,time);
sum +=199;
}else{
break;
}
}
try {
XMLWriter output = new XMLWriter(new FileWriter(file));
output.write(document);
output.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public void item(Element root,String time){
// 在根元素中使用 addElement()方法增加employee元素。
Element empElem =root.addElement(“Play”);
// 使用 addAttribute()方法向employee元素添加id和name属性
/* empElem.addAttribute(“id”,”0001″);
empElem.addAttribute(“name”,”wanglp”);*/
// 向employee元素中添加sex元素
Element ScreenElem =empElem.addElement(“ScreenMode”);
// 使用setText()方法设置sex元素的文本
ScreenElem.setText(“C1”);
Element NameElem =empElem.addElement(“Name”);
// 使用setText()方法设置sex元素的文本
NameElem.setText(“CCTV看点(17.05.10)C”);
Element TypeElem =empElem.addElement(“Type”);
// 使用setText()方法设置sex元素的文本
TypeElem.setText(“program”);
// 在employee元素中增加age元素 并设置该元素的文本。
Element FileNameElem =empElem.addElement(“FileName”);
FileNameElem.setText(“F682662E2A81E5D671897A4D5E5F3B89.mp4”);
Element SizeElem =empElem.addElement(“Size”);
SizeElem.setText(“40306524”);
Element LongElem =empElem.addElement(“Long”);
LongElem.setText(“199”);
Element StartElem =empElem.addElement(“Start”);
StartElem.setText(time);
Element ContractIDElem =empElem.addElement(“ContractID”);
ContractIDElem.setText(“”);
Element SecondScreen_e =empElem.addElement(“SecondScreen”);
Element aaName =SecondScreen_e.addElement(“Name”);
aaName.setText(“20170508巴士头条gif轮播图1\n” +
“20170508巴士头条gif轮播图2”);
Element aaIntervalTime =SecondScreen_e.addElement(“IntervalTime”);
aaIntervalTime.setText(“15”);
Element aaContractID =SecondScreen_e.addElement(“ContractID”);
aaContractID.setText(“”);
Element PictureName =SecondScreen_e.addElement(“Picture”);
Element FileName =PictureName.addElement(“FileName”);
FileName.setText(“2C4CC9E7A4543D9F8E37A96C542C7F2F.jpg”);
Element FileName1 =PictureName.addElement(“FileName”);
FileName1.setText(“2CB8B2F1C9533C26A61F07A39008CAC7.gif”);
Element Subtitle =empElem.addElement(“Subtitle”);
Element aaaName =Subtitle.addElement(“Name”);
aaaName.setText(“巴士头条20170508gif文案1.txt\n” +
“巴士头条20170508gif文案2.txt”);
Element aaSubtitleMode =Subtitle.addElement(“SubtitleMode”);
aaSubtitleMode.setText(“SS1”);
Element Content =Subtitle.addElement(“Content”);
Content.setText(“打开蓝牙,打开微信摇周边$%$\n” +
“海量福利尽在巴士头条$%$”);
Element ContractID =Subtitle.addElement(“ContractID”);
ContractID.setText(“”);
}
调用方法
[java] view plaincopyprint?
<code class=”language-java hljs”> String path =<span class=”hljs-string”>”/sdcard/”</span>;
<span class=”hljs-keyword”>final</span> File file = <span class=”hljs-keyword”>new</span> File(path, <span class=”hljs-string”>”hanshihuan.xml”</span>);
<span class=”hljs-keyword”>try</span> {
<span class=”hljs-keyword”>if</span>(!file.exists())
file.createNewFile();
} <span class=”hljs-keyword”>catch</span> (IOException e) {
<span class=”hljs-comment”>// TODO Auto-generated catch block</span>
e.printStackTrace();
}
<span class=”hljs-keyword”>new</span> Thread(<span class=”hljs-keyword”>new</span> Runnable() {
<span class=”hljs-meta”>@Override</span>
<span class=”hljs-function”><span class=”hljs-keyword”>public</span> <span class=”hljs-keyword”>void</span> <span class=”hljs-title”>run</span><span class=”hljs-params”>()</span> </span>{
createXml(file);
}
}).start();</code>
[java] view plaincopyprint?
<code class=”language-java hljs”> String path =<span class=”hljs-string”>”/sdcard/”</span>;
<span class=”hljs-keyword”>final</span> File file = <span class=”hljs-keyword”>new</span> File(path, <span class=”hljs-string”>”hanshihuan.xml”</span>);
<span class=”hljs-keyword”>try</span> {
<span class=”hljs-keyword”>if</span>(!file.exists())
file.createNewFile();
} <span class=”hljs-keyword”>catch</span> (IOException e) {
<span class=”hljs-comment”>// TODO Auto-generated catch block</span>
e.printStackTrace();
}
<span class=”hljs-keyword”>new</span> Thread(<span class=”hljs-keyword”>new</span> Runnable() {
<span class=”hljs-meta”>@Override</span>
<span class=”hljs-function”><span class=”hljs-keyword”>public</span> <span class=”hljs-keyword”>void</span> <span class=”hljs-title”>run</span><span class=”hljs-params”>()</span> </span>{
createXml(file);
}
}).start();</code>