Solved

XML in http message - example required

Posted on 2002-03-13
3
201 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_
Comment Utility
get some real xml parser engine and check the examples that come with it.
0
 
LVL 3

Expert Comment

by:saxaboo
Comment Utility
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 300 total points
Comment Utility
//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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
unix in java example 9 38
eclipse package explorer vs project explorer view 2 27
maven example error 3 34
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now