Use XPath in servlet : JDOM « XML « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » XML » JDOM 




Use XPath in servlet
   

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

public class PostServlet extends HttpServlet {

    private SAXBuilder builder = new SAXBuilder();

    private XPath nameXPath;

    private XPath yearXPath;

    protected void doPost(HttpServletRequest request,
            HttpServletResponse responsethrows ServletException, IOException {
        if (!"text/xml".equals(request.getContentType())) {
            response.getWriter().println("Please post as text/xml.");
        else {
            try {
                Document doc = builder.build(request.getReader());
                StringBuffer buff = new StringBuffer();
                buff.append("You searched for name '" + nameXPath.valueOf(doc)
                        "'");
                String year = yearXPath.valueOf(doc);
                if (!"notselected".equals(year)) {
                    buff.append(" and year '" + year + "'");
                }
                buff.append(".");
                response.getWriter().print(buff.toString());
            catch (JDOMException e) {
                response.getWriter().print(
                        "Error getting search terms: " + e.getMessage());
            }
        }
    }

    public void init() throws ServletException {
        try {
            nameXPath = XPath.newInstance("/search/name/text()");
            yearXPath = XPath.newInstance("/search/year/text()");
        catch (JDOMException e) {
            throw new ServletException("Unable to create XPaths", e);
        }
        super.init();
    }
}

   
    
  














Related examples in the same category
1.Simple demo of JDOM
2.Read an XML document using JDOM
3.Use JDOM to build a document
4.Make up and write an XML document, using JDOMMake up and write an XML document, using JDOM
5.List an XML file after building it into a JDOM DocumentList an XML file after building it into a JDOM Document
6.Simple example of using JDOM
7.Use Unchecked JDOM Factory
8.JDom: Locating a speech with the findCharactersFirstSpeech() method
9.Use JDOM to change the element text
10.JDOM: transform
11.Parsing with JDOM
12.Accessing Attributes Using JDOM
13.Creating a Document Using JDOM
14.Adding an Element Using JDOM
15.Adding an Attribute Using JDOM
16.Outputting a Document Using XMLOutputter
17.Use SAXBuilder from JDOM
18.Create document from jdom
19.Add elements to root element
20.Get child from root
21.XPath with JDOM
22.NamespaceTest with JDOM
23.extends org.jdom.Element
24.Iterate through elements
25.JDOM Util
26.General XML Utility Functions For JDOM
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.