[jdom-interest] creating files

Eric Hansen ephansen at pacbell.net
Sat Jul 5 09:23:31 PDT 2003


I've only been using JDom for a little while... but here is test class that you can run that shows how I've been doing it.  Maybe a seasoned expert may want to comment on whether my usage is appropriate...
-Eric


import org.jdom.Element;
import org.jdom.Document;

public class xmlDocumentTest {

    /**
     * create the jdom test document
     * 
     * <?xml version="1.0" encoding="ISO-8859-1" ?>
     * <PersonalData>
     *   <FirstName>fName</FirstName>
     *   <MiddleName>mName</MiddleName>
     *   <LastName>lName</LastName>
     * </PersonalData>
     * 
     * @param fName
     * @param mName
     * @param lName
     * @return
     */ 
    public Document buildSampleXml(String fName, String mName, String lName){

        Element root = new Element("PersonalData");     //

        Element node1 = new Element("FirstName");
        node1.addContent(fName);
        root.addContent(node1);

        Element node2 = new Element("MiddleName");
        node2.addContent(mName);
        root.addContent(node2);

        Element node3 = new Element("LastName");
        node3.addContent(lName);
        root.addContent(node3);

        // create the document
        return  new org.jdom.Document(root);
    }

    /**
     * parse the jdom document to extract the data of interest
     * @param doc
     * @return
     */ 
    public String readDocument(Document doc){

        StringBuffer fullName = new StringBuffer();

        Element root = doc.getRootElement();
        fullName.append(root.getChildText("FirstName") + " ");
        fullName.append(root.getChildText("MiddleName") + " ");
        fullName.append(root.getChildText("LastName"));

        return fullName.toString();

    }

    /**
     * pass the 3 arguments to execute the creating and reading of the jdom document
     * @param args
     */ 
    public static void main(String[] args){

        String fName = args[0];
        String mName = args[1];
        String lName = args[2];

        xmlDocumentTest o = new xmlDocumentTest();
        Document xml = o.buildSampleXml(fName, mName, lName);
        String xmlString = o.readDocument(xml);

        System.out.println(xmlString);

    }



  ----- Original Message ----- 
  From: salil khanwalkar 
  To: jdom-interest at jdom.org 
  Sent: Saturday, July 05, 2003 8:20 AM
  Subject: [jdom-interest] creating files


  Hi,

  I am new to XML
  I want to create a file which has the first,middle,last names of a person.Then i want to read the file and take individual values of the first,middle,last name. Can anybody suggest me how to create this kind of a file.

  Thank You,
  Salil Khanwalkar.



------------------------------------------------------------------------------
  Do you Yahoo!?
  SBC Yahoo! DSL - Now only $29.95 per month!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20030705/c0845b5c/attachment.htm


More information about the jdom-interest mailing list