?
Solved

constructing xml message

Posted on 2004-11-23
7
Medium Priority
?
304 Views
Last Modified: 2013-11-23
Hi,

Can anyone assist on constructing an xml message.

I have an html form which has around 10 fields on submit the value is posted to a servlet which
should construct the xml message from the request parameter.

Is there any examples where I can get some info.

Thanks
Bangviz
0
Comment
Question by:bangviz
7 Comments
 
LVL 1

Author Comment

by:bangviz
ID: 12662328
oops my question once again

Hi,

Case1
Can anyone assist on constructing an xml message.

I have an html form which has around 10 fields on submit the value is posted to a servlet which
should construct the xml message from the request parameter.

Is there any examples where I can get some info.

Case2
Parsing the same xml message and displaying it on the jsp


Thanks
Bangviz
0
 
LVL 13

Accepted Solution

by:
petmagdy earned 200 total points
ID: 12662560
0
 
LVL 2

Assisted Solution

by:sujithkumars123
sujithkumars123 earned 800 total points
ID: 12662572
loop the request parameter names. add the name and value to the tags

StringBuffer xml = new StringBuffer();
xml.append(<params>);
Enumeration enum = req.getParameterNames();
while (enum.hasMoreElements()) {
 String name = (String)enum.nextElement();
 String value = req.getParameter(name);

 xml.append("<param>");
 xml.append("<name>");
 xml.append(name);
 xml.append("</name>");
 xml.append("<value>");
 xml.append(value);
 xml.append("</value>");
 xml.append("</param>");
}

xml.append(</params>);

This will construct your xml message for the request paramters.

Now you can write a parser for the xml file.
Display the name and value of each param.
0
Industry Leaders: 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!

 
LVL 1

Author Comment

by:bangviz
ID: 12664190
BTW I have different field name,

Do i need to pass any sort of keyvalue in my html

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

                  <TR>
                        <TD>Name</TD>
                        <TD>
                              <INPUT TYPE=TEXT NAME="Name" ID="name">
                        </TD>
                  </TR>
                  <TR>
                        <TD>Address</TD>
                        <TD>
                              <INPUT TYPE=TEXT NAME="Address" ID="address">
                        </TD>
                  </TR>
                  <TR>
                        <TD>City</TD>
                        <TD>
                              <INPUT TYPE=TEXT NAME="City" ID="city">
                        </TD>
                  </TR>
                  <TR>
                        <TD>State</TD>
                        <TD>
                              <INPUT TYPE=TEXT NAME="State" ID="state">
                        </TD>
                  </TR>
                  <TR>
                        <TD>email</TD>
                        <TD>
                              <INPUT TYPE=TEXT NAME="email" ID="email">
                        </TD>
                  </TR>

      <INPUT TYPE=SUBMIT NAME="submit" ID="submit" VALUE="Submit">
      <INPUT TYPE=RESET NAME="reset" ID="reset" VALUE="Reset">
      </FORM>
0
 
LVL 3

Expert Comment

by:drjustin
ID: 12668090
Look at
 - xml.apache.org/xmlbeans/
 - xstream.codehaus.org/

Those are quite capable xml parsing libraries. Converting that to html should be fairly easy.
0
 
LVL 1

Author Comment

by:bangviz
ID: 12692679
I have resolved most of it..does anyone can provide a link to download SAXParser which is part of "com.ibm.xml.parsers.SAXParser"

If not how can i resolve in my code as shown below...here AddressBean is a value object with getset method...

Anticipating to hear
=========================

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.SAXParser;

import org.xml.sax.AttributeList;
import org.xml.sax.HandlerBase;
import org.xml.sax.InputSource;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;

public class ResponseParser extends HandlerBase{

      private Parser parser;
       private StringBuffer sb = new StringBuffer(100);
       protected String xmlMessage;
       protected AddressBean directBean;
/**
 * ResponseParser constructor.
 */
public ResponseParser( String _xmlMessage, AddressBean _directBean ) throws SAXException, IOException {

      super();
      parser = new SAXParser();
      parser.setDocumentHandler( this );
      xmlMessage = _xmlMessage;
      directBean = _directBean;
}
public void characters(char[] buf, int offset, int len) throws org.xml.sax.SAXException {

      sb.append( buf, offset, len );
}
public void endElement(String tag) throws org.xml.sax.SAXException {
      
      String data = sb.toString().trim();

      directBean.getDataMap().put(tag,data);
      
      
}
public void parse() throws SAXException, IOException {

      ByteArrayInputStream stream = new ByteArrayInputStream( xmlMessage.getBytes() );
      InputSource input =      new InputSource( stream );
      parser.parse( input );
}
public void startElement(String tag, AttributeList attrs) throws org.xml.sax.SAXException {
      
      sb = new StringBuffer( 100 );
}
}

0
 
LVL 1

Author Comment

by:bangviz
ID: 12692847
any ideas where i can download  SAXParser which is part of "com.ibm.xml.parsers.SAXParser"

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month17 days, 6 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