Import java. Io. file;
Import java. Io. filewriter;
Import java. Io. ioexception;
Import java. util. List;
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;
Import org. dom4j. Io. xmlwriter;
Import com. XM. ITV. entity. iptvmembers;
/**
* Create XML in dom4j
* @ Author Chunmei Xie
*
*/
Public class test {
Protected final log = logfactory. getlog (getclass ());
/**
* The method can be defined by the user according to the project requirements.
* @ Param Member
* @ Param checks
* @ Param medicode
*/
Public void sharfriend (iptvmembers Member, list <iptvmembers> checks, string medicode ){
// Create a heel Node
Org. dom4j. Document document = org. dom4j. documenthelper. createdocument ();
// Add a node named sharerequest
Org. dom4j. Element sharerequestelement = Document. addelement ("sharerequest ");
// Add a subnode from in sharerequest
Org. dom4j. Element formelement = sharerequestelement. addelement ("from ");
If (member! = NULL ){
// Write the value to the from node. Addtext
Formelement. addtext (member. getuid (). tostring ());
} Else
{
Formelement. addtext ("");
}
If (checks! = NULL & checks. Size ()> 0 ){
// Multiple to subnodes can be written in a loop.
For (INT I = 0; I <checks. Size (); I ++ ){
// Write to subnode to sharerequest Node
Org. dom4j. Element toelement = sharerequestelement. addelement ("");
If (checks. Get (I )! = NULL & checks. Get (I). getfrienduid ()! = NULL)
// Write text information to the to node.
Toelement. addtext (checks. Get (I). getfrienduid (). tostring ());
}
} Else
{
Org. dom4j. Element toelement = sharerequestelement. addelement ("");
Toelement. addtext ("");
}
// Write the moviecode node in the same sharerequest
Org. dom4j. Element movieelement = sharerequestelement. addelement ("moviecode ");
If (medicode! = NULL ){
// Add Value
Movieelement. addtext (medicode );
} Else
{
Movieelement. addtext (medicode );
}
Try {
// Create an object. The path can be defined by yourself. This is the relative path. After running, the file will be created in the same class and package of the project in Tomcat.
Xmlwriter output = new xmlwriter (
New filewriter (new file (this. getclass (). getresource (file. separator). getpath () + "sharfriend. xml ")));
// Write the created XML Information
Output. Write (document );
Log.info ("-- sharfriend --");
Output. Close ();
}
Catch (ioexception e ){
Log.info (E. getmessage ());
}
}
}
The creation result is:
<? XML version = "1.0" encoding = "UTF-8"?>
<Sharerequest> <from> 2653 </from> <to> 664 </to> <moviecode> test </moviecode> </sharerequest>