Solved

XML in http message - example required

Posted on 2002-03-13
3
211 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 300 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 36
fibonacci ten numbers 4 38
HashTable highest marks enumeration alternative 9 37
servlet web applications   metadata-complete="true" or false 3 37
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

806 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