Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

construct xml using blank template

Posted on 2004-12-01
4
Medium Priority
?
287 Views
Last Modified: 2013-11-19
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>

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

0
Comment
Question by:bangviz
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 12714592
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
 
LVL 1

Author Comment

by:bangviz
ID: 12719659
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
 
LVL 1

Author Comment

by:bangviz
ID: 12720052
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
 
LVL 21

Accepted Solution

by:
MogalManic earned 1200 total points
ID: 12727180
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question