Create an XML document in dom4j

Source: Internet
Author: User
Tags processing instruction ibm developerworks

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 ();

}
}

 

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.