Java generates XML files

Source: Internet
Author: User

Because of the work required, the following XML needs to be generated:

<?XML version= "1.0" encoding= "UTF-8"?><Root><ScormTheme= "2016-06-22 18:09:45"Speakor=""Introduction=""Scromtype= "1"><videolistName= "Channel 1"Count= "1"><VideoFile= "111.mp4" Time= "118000"/></videolist><videolistName= "Channel 2"Count= "1"><VideoFile= "222.mp4" Time= "118000"/></videolist><indexlistCount= "0"/><ChangelistCount= "0"/></Scorm></Root>
View Code

Here's how to write:

 Public voidcreatxml (String path, string[] names, string[] timelengths)throwsIOException {Path= path + "\\assets\\"; Document Doc=documenthelper.createdocument (); //Add root nodeElement root = doc.addelement ("root"); //Add child elementsElement scorm = root.addelement ("Scorm"); //to add a property to a child nodeScorm.addattribute ("Theme", "" "); Scorm.addattribute ("Speakor", "" "); Scorm.addattribute ("Introduction", "" "); Scorm.addattribute ("Scromtype", "1");  for(inti = 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", "channel" + (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"); //instantiating an output format objectOutputFormat format =Outputformat.createprettyprint (); //Set Output EncodingFormat.setencoding ("UTF-8"); //Create a file object to write toFile File =NewFile (path + file.separator + "Videolist.xml"); //generate XmlWriter objects, parameters in constructors are file streams and formats that require outputXMLWriter writer =NewXMLWriter (Newfileoutputstream (file), format); //start writing, the Write method contains the Document object created aboveWriter.write (DOC); }
View Code

Call:

1 string[] names = {"1 channel", "2 Channel", "3 Channel"}; 2 string[] timelengths = {"$", "$", "$"}; 3 String path = "e:\\video\\"; 4 5 creatxml (path,names,timelengths);
View Code

Java generates XML files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.