Solved

XML in http message - example required

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

914 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

12 Experts available now in Live!

Get 1:1 Help Now