Java使用jdom生成xml

December 15, 2011 | tags      | views
Comments 0

package com.just.xml;

 

import java.io.FileOutputStream;

import java.io.IOException;

import   org.jdom.*;

import   org.jdom.input.*;

import   org.jdom.input.*;

import   org.jdom.output.*;

 

public class BuildXML {

 

public   void   BuildXMLDoc()   throws   IOException,   JDOMException {

        Element   eeeRoot, rows,  flag,   content;

        Document   Doc;

        eeeRoot   =   new   Element("employees_information");

        Doc   =   new   Document(eeeRoot);

        eeeRoot   =   Doc.getRootElement();

        

        rows = new Element("rows");

        

        flag   =   new   Element("name");

        content   =   flag.setText("C.Y.   Shen");

        //content   =   flag.addAttribute("emp_id",   "001");

        flag   =   rows.addContent(content);

        flag   =   new   Element("age");

        content   =   flag.setText("43");

        flag   =   rows.addContent(content);

        flag   =   new   Element("sex");

        content   =   flag.setText("Male");

        flag   =   rows.addContent(content);

        

        rows = eeeRoot.addContent(rows);

        

        //   XMLOut.setEncoding("gb2312");

        XMLOutputter   XMLOut   =   new   XMLOutputter();

        //XMLOut.output(Doc,   new   FileOutputStream("c://test1.xml"));

        

        System.out.println(XMLOut.outputString(Doc));

        

    }

    public   static   void   main(String[]   args) {

        try   {

         BuildXML   s1   =   new   BuildXML();

            System.out.println("Now   we   build   an   XML   document   .....");

            s1.BuildXMLDoc();

            

        } catch   (Exception   e) {

            System.out.println(e.getMessage());

        }

    }

 

}

 



文章本月排行 文章本年排行
   



发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。