?
Solved

XML in http message - example required

Posted on 2002-03-13
3
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 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