[jdom-interest] JDOM and a servlet example
Bolt, Dave
boltd at atsc.army.mil
Fri Aug 11 12:41:14 PDT 2000
Joan is this something like what you wanted?
Dave
package org.jdom.samples.servlet;
/*
Notes:
1) This is a basic servlet created with JBuilder3's servlet wizard
The servlet responds to HTTP GET requests that have a parameter called
xmlDocument which is the URL of an XML document. The servlet simply
shows all of the address entries in an HTML table
2) Obviously a better example would make use of XSLT
3) To get this code to work with your XML document, I had to delete the
internal DTD definition. It caused the parsing to fail. This code be due
to the fact that I'm running the CVS version of JDOM on my PC and it might
be broken in this release.
*/
/* Here is the XML file that I used
<?xml version= "1.0" standalone="yes"?>
<ADDRESSBOOK>
<PERSON>
<LASTNAME>Miyamato</LASTNAME>
<FIRSTNAME>Musashi</FIRSTNAME>
<COMPANY>Bushido, Inc.</COMPANY>
<EMAIL>katana at bushido.com</EMAIL>
</PERSON>
<PERSON>
<LASTNAME>Einstein</LASTNAME>
<FIRSTNAME>Albert</FIRSTNAME>
<COMPANY>Hasta Proton, Inc.</COMPANY>
<EMAIL>hasta at proton.com</EMAIL>
</PERSON>
<PERSON>
<LASTNAME>Hampton</LASTNAME>
<FIRSTNAME>Ed</FIRSTNAME>
<COMPANY>AMAC, Inc.</COMPANY>
<EMAIL>energy at hsingi.com</EMAIL>
</PERSON>
<PERSON>
<LASTNAME>wer</LASTNAME>
<FIRSTNAME>werwer</FIRSTNAME>
<COMPANY>wer</COMPANY>
<EMAIL>erwer</EMAIL>
</PERSON>
</ADDRESSBOOK>
*/
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;
import org.jdom.*;
import org.jdom.input.*;
public class xmlServlet extends HttpServlet {
//Initialize global variables
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
//URL to XML file
String xmlUrl = "";
try { xmlUrl = request.getParameter("xmlDocument"); } catch (Exception
e) { e.printStackTrace(); }
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
//
// Send out the beginnings of the page
//
out.println("<html>");
out.println("<head><title>JDOM Servlet Example</title></head>");
//
out.println("<body>");
out.println("<table border = 1>");
out.println("<tr><th>Last Name</th><th>First
Name</th><th>Company</th><th>e-mail</th></tr>");
//
// Get a handle to the XML document
//
try
{
//
// Construct a URL pointing to the XML document
//
URL theDoc = new URL(xmlUrl);
//
SAXBuilder builder = new SAXBuilder(); // Note: NO Validation is
performed here
Document addressBook = builder.build(theDoc);
//
// addressBook now contains the JDOM model of the AddressBook document
//
// Get the root element
//
Element root = addressBook.getRootElement();
//
// Get all of the Person entries
//
List persons = root.getChildren("PERSON");
//
// Iterate through the persons list
//
Iterator itr = persons.iterator();
//
while (itr.hasNext())
{
Object o = itr.next();
Element person = (Element)o;
//
StringBuffer row = new StringBuffer("<tr>");
//
// Note: getChildText is a convenience method to get the data
// for a child of an element it is semantically equivalent to
// person.getChild("LASTNAME").getText();
//
row.append("<td>" + person.getChildText("LASTNAME") + "</td>");
row.append("<td>" + person.getChildText("FIRSTNAME") + "</td>");
row.append("<td>" + person.getChildText("COMPANY") + "</td>");
row.append("<td><A HREF=\"mailto:" + person.getChildText("EMAIL") +
"\">" +person.getChildText("EMAIL") + "</a></td>");
row.append("</tr>");
out.println(row.toString());
//
}
out.println("</table>");
out.println("</body></html>");
}
catch (MalformedURLException e) {
e.printStackTrace();}
catch (JDOMException e) {
e.printStackTrace();}
finally
{
out.close();
}
}
//Get Servlet information
public String getServletInfo() {
return "org.jdom.samples.servlet Information";
}
}
-----Original Message-----
From: joan [mailto:joan13500 at yahoo.com]
Sent: Friday, August 11, 2000 2:39 PM
To: jdom-interest at jdom.org
Subject: [jdom-interest] JDOM and a servlet example
thanks to people who respond to my question,
I new in Java programming but I know well xml and xslt
In fact, it would be usefull for me to find a complete example of using JDOM
with a servlet.
I'm sure it's not very different than using it on command line but after
many tries I'm not able to convert
the Jdom examples in servlets...
May someone write for me a very simple code (but complete) of how read a
basic xml file (ex: AddressBook.xml) and view it in a browser with a
servlet.
I'm sure all will be very clear for me with a complete example...
here this the AddressBook.xml file I'd like to view in my browser with JDOM:
<?xml version= '1.0'?>
<!DOCTYPE ADDRESSBOOK [
<!ELEMENT ADDRESSBOOK (PERSON)*>
<!ELEMENT PERSON (LASTNAME, FIRSTNAME, COMPANY, EMAIL)>
<!ELEMENT LASTNAME (#PCDATA)>
<!ELEMENT FIRSTNAME (#PCDATA)>
<!ELEMENT COMPANY (#PCDATA)>
<!ELEMENT EMAIL (#PCDATA)>
]>
<ADDRESSBOOK>
<PERSON>
<LASTNAME>Miyamato</LASTNAME>
<FIRSTNAME>Musashi</FIRSTNAME>
<COMPANY>Bushido, Inc.</COMPANY>
<EMAIL>katana at bushido.com</EMAIL>
</PERSON>
<PERSON>
<LASTNAME>Einstein</LASTNAME>
<FIRSTNAME>Albert</FIRSTNAME>
<COMPANY>Hasta Proton, Inc.</COMPANY>
<EMAIL>hasta at proton.com</EMAIL>
</PERSON>
<PERSON>
<LASTNAME>Hampton</LASTNAME>
<FIRSTNAME>Ed</FIRSTNAME>
<COMPANY>AMAC, Inc.</COMPANY>
<EMAIL>energy at hsingi.com</EMAIL>
</PERSON>
<PERSON>
<LASTNAME>wer</LASTNAME>
<FIRSTNAME>werwer</FIRSTNAME>
<COMPANY>wer</COMPANY>
<EMAIL>erwer</EMAIL>
</PERSON>
</ADDRESSBOOK>
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com
More information about the jdom-interest
mailing list