import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.xerces.parsers.DOMParser; import org.w3c.dom.*; import org.xml.sax.*; import org.xml.sax.helpers.*; public class LhairesXMLserv extends HttpServlet{ //Global variables PrintWriter out; String xmlURL = "http://tecfa.unige.ch/staf/staf-e/lhaires/staf2x/example.xml"; int after = 0; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { setPrintWriter(response.getWriter()); out.println("Error: don't use GET requests"); } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); setPrintWriter (response.getWriter()); try { out.println("
No file specified: processing default file "); out.println("" + xmlURL + "
"); processXml(xmlURL); } else processXml(rep); } catch (Exception e){ out.println("Exception found:"); out.println(e.getMessage()); e.printStackTrace(out); out.println(""); } } public void processXml(String file){ try { // create a handler DOMParser parser = new DOMParser(); // DO IT parser.parse(file); Document doc = parser.getDocument(); Element root = (Element)doc.getDocumentElement(); out.println("
Article | Description | "); out.println("Fournisseur | Items in stock | Prize in SFR |
---|---|---|---|---|
"); } else if (name.equalsIgnoreCase("descr")){ out.println(" |
| "); } else if (name.equalsIgnoreCase("stock")) out.println(" | "); else if (name.equalsIgnoreCase("prix")) out.println(" | "); } else out.println("empty node!!!"); Node ch = node.getFirstChild(); if (ch != null){ String val = ch.getNodeValue(); val = val.trim(); if (! val.equals("")) out.println(val); if (node.hasChildNodes()){ NodeList nodes = node.getChildNodes(); int nodesNr = nodes.getLength(); for (int i = 0; i < nodesNr; i++) displayTree(nodes.item(i)); } } } else out.println("Null found"); } public void closeFile(){ out.print(" |