JAVA產生XML檔案

來源:互聯網
上載者:User

標籤:nbsp   print   調用   leo   sed   closed   code   throw   ide   

由於工作需要,需要產生如下xml:

<?xml version="1.0" encoding="UTF-8"?><root><Scorm Theme="2016-06-22 18:09:45" Speakor="" Introduction="" ScromType="1"><VideoList Name="通道1" Count="1"><Video File="111.mp4" Time="118000"/></VideoList><VideoList Name="通道2" Count="1"><Video File="222.mp4" Time="118000"/></VideoList><IndexList Count="0"/><ChangeList Count="0"/></Scorm></root>
View Code

編寫方法如下:

public void creatXML(String path, String[] names, String[] timelengths)            throws IOException {        path = path + "\\assets\\";        Document doc = DocumentHelper.createDocument();        // 增加根節點        Element root = doc.addElement("root");        // 增加子項目        Element scorm = root.addElement("Scorm");        // 為子節點添加屬性        scorm.addAttribute("theme", "");        scorm.addAttribute("speakor", "");        scorm.addAttribute("introduction", "");        scorm.addAttribute("ScromType", "1");        for (int i = 0; i < names.length; i++) {            String name = names[i].substring(names[i].lastIndexOf("\\") + 1,                    names[i].length());            Element videoList = scorm.addElement("VideoList");            videoList.addAttribute("Name", "通道" + (i + 1));            videoList.addAttribute("Count", "1");            Element video = videoList.addElement("Video");            video.addAttribute("File", name);            video.addAttribute("Time", timelengths[i]);        }        Element indexList = scorm.addElement("IndexList");        indexList.addAttribute("Count", "0");        Element changeList = scorm.addElement("ChangeList");        changeList.addAttribute("Count", "0");        // 執行個體化輸出格式對象        OutputFormat format = OutputFormat.createPrettyPrint();        // 設定輸出編碼        format.setEncoding("UTF-8");        // 建立需要寫入的File對象        File file = new File(path + File.separator + "videoList.xml");        // 產生XMLWriter對象,建構函式中的參數為需要輸出的檔案流和格式        XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);        // 開始寫入,write方法中包含上面建立的Document對象        writer.write(doc);    }
View Code

調用:

1 String[] names = {"1通道","2通道","3通道"};2 String[] timelengths = {"500","500","500"};3 String path = "E:\\video\\";4 5 creatXML(path,names,timelengths);
View Code

 

JAVA產生XML檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.