construct xml using blank template

I have an address.html which submits the form data to a Addressservlet,
which need to construct the message.xml using a blank template of xml message
as shown below and write the data received from the form to the corresponding
xml tag in message template.

Kindly assist with some code to the servlet. Its very urgent

=================
address.html

<FORM METHOD=POST ACTION="/servlet/AddressServlet">

               <TR>
                    <TD>Name</TD>
                    <TD>
                         <INPUT TYPE=TEXT NAME="Name1" >
                    </TD>
               </TR>
               <TR>
                    <TD>Mobile</TD>
                    <TD>
                         <INPUT TYPE=TEXT NAME="Mobile1">
                    </TD>
               </TR>

               <TR>
                    <TD>Name</TD>
                    <TD>
                         <INPUT TYPE=TEXT NAME="Name2" >
                    </TD>
               </TR>
               <TR>
                    <TD>Mobile</TD>
                    <TD>
                         <INPUT TYPE=TEXT NAME="Mobile2">
                    </TD>
               </TR>

               <TR>
                    <TD>Number</TD>
                    <TD>
                         <INPUT TYPE=TEXT NAME="Number">
                    </TD>
               </TR>
     <INPUT TYPE=SUBMIT NAME="submit" ID="submit" VALUE="Submit">
     <INPUT TYPE=RESET NAME="reset" ID="reset" VALUE="Reset">
     </FORM>

=================
Message.xml

<message>
<type>contractcreateJ07</type>
<Header>
<UserId>AW</UserId>
<Pwd>Pwd</Pwd>
</Header>
<Property>
<Version>1.0</Version>
</Property>
<Data>
<Person1>
<Name> </Name>
<Mobile> </Mobile>
</Person1>
<Person2>
<Name> </Name>
<Mobile> </Mobile>
</Person2>
<Details>
<Number> </Number>
</Details>
<message>

================

LVL 1
bangvizAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MogalManicCommented:
I would recommend using JDOM to parse the XML and traverse to the appropriate node.  The code would be something like this:
      Document doc;
      SAXBuilder builder=new SAXBuilder();
      doc=builder.build(template);
      
      Element docRoot=doc.getRootElement();
      
      Element nameElement = docRoot.getChild("Person1").getChild("Name");
      nameElement.setText(request.getParameter("Name1"))
                ... more code for the rest...
0
bangvizAuthor Commented:
I have done this can any expert advice if any changes on this code...
Basically it should write to the xml file and send the xml to the queue.

Please verify and post the comments its urgent do i need

import java.util.Enumeration;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.servlet.http.HttpServletRequest;

//getting the requ est from servlet and enumerating it
    public String buildMessage(HttpServletRequest req, Enumeration enum)
    {
         StringBuffer address = new StringBuffer("");
         
         System.out.println("Starting to build the message");
         
         Document doc;
       SAXBuilder builder=new SAXBuilder();

//Here Message.xml is the template as shown above
       doc=builder.build("c:\message.xml");
                 
       Element docRoot=doc.getRootElement();

         enum = req.getParameterNames();

         System.out.println("enumerating person1");                
         while (enum.hasMoreElements())
         {
             String name = (String) enum.nextElement();
             String value = req.getParameter(name);

//writing to the template                
                 if(name.equals("Name1"))
                 {
             Element nameElement = docRoot.getChild("Person1").getChild("Name");
             nameElement.setText(request.getParameter("Name1"))
                 }// end if
               
             else if(name.equals("Mobile1"))
                 {
             Element nameElement = docRoot.getChild("Person1").getChild("Mobile");
             nameElement.setText(request.getParameter("Mobile1"))
                 }// end if

                 if(name.equals("Name2"))
                 {
             Element nameElement = docRoot.getChild("Person2").getChild("Name");
             nameElement.setText(request.getParameter("Name2"))
                 }// end if
               
             else if(name.equals("Mobile2"))
                 {
             Element nameElement = docRoot.getChild("Person2").getChild("Mobile");
             nameElement.setText(request.getParameter("Mobile2"))
                 }// end if

             else if(name.equals("Number"))
                 {
             Element nameElement = docRoot.getChild("Details").getChild("Number");
             nameElement.setText(request.getParameter("Number"))
                 }// end if

//Is this correct, sending the file to queue
         return address.toString();
    }

0
bangvizAuthor Commented:
oops I have another method to send the file to the queue..all i need in the above code is write to the file and return the xmlmessage with all the input posted data from the form.

There by my next method will actually send the xml message returned by above method build message.

Any expert please comment on it and points will be shared...its urgent...
0
MogalManicCommented:
One improvement you can make is to refactor the repeated code into a loop like this:
    public String buildMessage(HttpServletRequest req) throws JDOMException, IOException
    {

        System.out.println("Starting to build the message");

        Document doc;
        SAXBuilder builder = new SAXBuilder();

//Here Message.xml is the template as shown above
        doc = builder.build("c:\\message.xml");

        Element docRoot = doc.getRootElement();


        System.out.println("enumerating person1");
        boolean done = false;
        final int MAX_PEOPLE = 2;
        for (int personNum = 0; personNum < MAX_PEOPLE; personNum++)
        {
            final String name = req.getParameter("Name" + personNum);
            final String mobile = req.getParameter("Mobile" + personNum);
            if (name == null)
                break;  //No more names
            final Element personElement = docRoot.getChild("Person" + personNum);
            Element nameElement = personElement.getChild("Name");
            nameElement.setText(req.getParameter(name));

            Element mobileElement = personElement.getChild("Name");
            mobileElement.setText(req.getParameter(name));

        }

        XMLOutputter out = new XMLOutputter();
        final StringWriter address = new StringWriter();
        out.output(doc, address);
        return address.toString();
    }


To output the document as a string you need to do what I did at the bottom of the function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.