?
Solved

XML in http message - example required

Posted on 2002-03-13
3
Medium Priority
?
229 Views
Last Modified: 2013-11-24
I need to build a web service using Tomcat, Servlets, JDBC etc. I also need to have something to show my client in short order! Can anyone provide me with an example servlet that takes XML from an HTTP post and parses it. Once it's parsed I'm happy I'll be able to process it.

I've got Tomcat 3.2.1 ( I believe - I'm not near the machine at the moment ) and it appears to have an old(ish) version of the xml parser on it. It's in the org/apache/tomcat/util/xml directory and comprises the following files
PathMatch.java
SaxContext.java
XmlAction.java
XmlMapper.java
XmlMatch.java

Any help is greatly appreciated

Regards

Dave
0
Comment
Question by:howesd
3 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 6863923
get some real xml parser engine and check the examples that come with it.
0
 
LVL 3

Expert Comment

by:saxaboo
ID: 6864098
Hi,

Check out the Apache SOAP toolkit at http://xml.apache.org

As far as I remember, it has some ready-to-deploy sample web services that you can plug into Tomcat, and tweak a little so your customer thinks you built it with your little hands :)

Hope this helps,

-S
0
 

Accepted Solution

by:
illumina earned 900 total points
ID: 6881059
//Using JDOM from jdom.org makes XML parsing beautifully simple, I use it for all my XML work. Without it
 //working with XML is twice as hard.
 //You'll also want to download the latest version of the free Xerces parser from xml.apache.org
 
 
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.adapters.*;
                 
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();                        
        Document doc;

         try {
              DOMBuilder builder = new DOMBuilder();
              //this parses the XML into a JDOM Dcument object
              doc = builder.build(request.getParameter("XMLFieldFromHTMLForm"));
              Element root = doc.getRootElement();
              //onc eyou have the root element you can access elements specifically, or interate through all of them. example below.
              java.util.List items = root.getChildren();
              Iterator i = items.iterator();
                  while (i.hasNext()) {
                      //do whaterver you want here to each node inthe XML
                      //read the JDOM docs for the methods to operate on XML nodes
                      Element elem = (Element)i.next();
                      out.println(elem.getChild("NameOfXMLNode").getText()) ;

                  }
             
       }catch (JDOMException e) {
            mainServer.log(this.getName(), "JDOMException trying to build DOM");
            e.printStackTrace();
        }

    }

0

Featured Post

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!

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 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