Import org. dom4j. Document;
Import org. dom4j. documenthelper;
Import org. dom4j. element;
Import java. Io .*;
Import org. dom4j. Io. xmlwriter;
Class dom4jparse
{
Public void generatedocument (){
// Use the explorenthelper class to create a document instance. Document is the dom4japi factory class for generating XML document nodes.
Document document = incluenthelper. createdocument ();
// Use the addelement () method to create the root element catalog. addelement () to add elements to the XML document.
Element catalogelement = Document. addelement ("catalog ");
// Add the comment "an XML catalog" using addcomment () in the catalog element ".
Catalogelement. addcomment ("an XML catalog --- >>>> liuchao ");
// Use addprocessinginstruction () in the catalog element to add a processing instruction.
Catalogelement. addprocessinginstruction ("target", "text ");
// Use the addelement () method to add the journal element in the catalog element.
Element journalelement = catalogelement. addelement ("journal ");
// Use the addattriher () method to add the title and publisher attributes to the journal element.
Journalelement. addattribute ("title", "XML zone ");
Journalelement. addattriher ("publisher", "IBM developerworks ");
// Add the journal element to the article element.
Element articleelement = journalelement. addelement ("article ");
// Add the level and date attributes for the article element.
Articleelement. addattriater ("level", "intermediater ");
Articleelement. addattribute ("date", "December-2001 ");
// Add the title element to the article element.
Element titleelement = articleelement. addelement ("title ");
// Use settext () to set the text of the article element.
Titleelement. settext ("Java configuration with XML schema ");
// Add the author element to the article element.
Element authorelement = articleelement. addelement ("author ");
// Add the firstname element to the author element and set the text of the element.
Element firstnameelement = authorelement. addelement ("firstname ");
Firstnameelement. settext ("Marcello ");
// Add the lastname element to the author element and set the text of the element.
Element lastnameelement = authorelement. addelement ("lastname ");
Lastnameelement. settext ("liuchao ");
//////////////////////////////////////// /////
Document. adddoctype ("catalog ",
Null, "file: // C:/dtds/CATALOG. DTD ");
Try {
Xmlwriter output = new xmlwriter (
New filewriter (new file ("D:/dom4jtext/dom4j. xml ")));
Output. Write (document );
Output. Close ();
}
Catch (ioexception e ){
System. Out. println (E. getmessage ());
}
}
Public static void main (string ARGs []) {
Dom4jparse dom4j = new dom4jparse ();
Dom4j. generatedocument ();
}
}