package net.programmera.www.dom; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.w3c.dom.Document; public class MyDOMServlet extends HttpServlet{ // ============================= // Inits the servlet // This will get done only once // ============================= public void init (ServletConfig config) throws ServletException { super.init(config); } public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { // =========================== // 1. Always do this // =========================== PrintWriter out=res.getWriter(); out.println("My DOM Parser"); out.println("

My DOM Parser

"); String xmlurl,normalize; // Get parameters xmlurl = req.getParameter ("xmlurl"); // =========================== // 2. Check if the user posted a form // =========================== if (xmlurl != null && ( xmlurl.length() > 1)){ MyDOMParser mdp= new MyDOMParser(); Document doc= mdp.parse(xmlurl); if(doc != null){ int normInt = Integer.parseInt(req.getParameter ("normalize")); if (normInt == 1) { out.println("
Normalizing"); doc.normalize(); }else{ out.println("
No normalizing"); } String outStr=mdp.getStructure(doc); String[] arrStr= outStr.split("#"); out.println("
============================="); out.println("
=== "+xmlurl +"==="); out.println("
============================="); for(int i=0; i< arrStr.length; i++){ out.println("
"+arrStr[i]); } out.println("
=============================


"); } } // =========================== // 3. Now, send the HTML to browser. // =========================== // Show DOM parser form. out.println("Enter the URL of the XML document you wish to parse .
"); out.println("
"); out.println("
URL: "); out.println("
Normalize: "); out.println("

"); out.println("
"); out.println(""); out.println(""); } }