// Tools used Eclipse http://www.eclipse.org//jdom http: //www.jdom.orgpackage com.test.jdom; import java.io.iOException; import java.util.hashmap; import java.util .Map; import org.jdom.Attribute; import org.jdom.DocType; import org.jdom.Document; import org.jdom.Element; import org.jdom.ProcessingInstruction; import org.jdom.output.Format; import org. JDM.Output.xmloutputter;
Public class product (String [] args) throws ioException {document doc = new document (); // Create a blank document / * * Create Pi and add to document * / map map = new hashmap (); MAP .put ("type", "text / xsl"); map.put ("href", "products.xsl"); processinginstruction pi = new processinginstruction ("XML-Stylesheet", Map); // Processing Instruction // Add a Doc.addContent (PI); / * * Create a document type and add a document * / docty Type = New DOCTYPE ("ProductsDetails); // Document Type Type.SetPublicId (" public.dtd "); / / Set to public //type.setsystemId ("system.dtd "); // Set to System // Add Document Type Doc.AddContent (Type); Element Root = New Element (" ProductsDetails); // Create an element Doc.sellElement (root); // uses the element as the root element element product product = new element ("product"); // PRIPUCT (PRODUCT); // Time Product as ProductsDetails Attribute Att = New Attribute ( "ProductID", "0001"); // Creating Properties Product.SetAttribute (ATT); / / Setting the Product Setting Property / / Creating a child element for Product, and sets its content to 100.00, Red Product.AddContent (New Element) ("Rate"). settext ("100.0 0 ")); Product.AddContent (New Element). Settext (" Red ")); / * * Format Output * / XMloutPutter Outp = New XMLOUTPUTTER (); // Used to Output Jdom Document Format Format = Format.getPrettyFormat (); // Format document format.setencoding ("GBK"); // Since the default code is UTF-8, Chinese will appear as garbled, so set to GBK OUTP.SETFORMAT (Format); OUTP .output (doc, system.out); // Output document}} The result is as follows: XML Version = "1.0" encoding = "gbk"?> Xml-stylesheet href = "products.xsl" type = "text / xsl ">