JSP Java Tutorial

Jsp code


<%@ page import="javax.xml.parsers.*, org.xml.sax.*, org.xml.sax.helpers.*, org.w3c.dom.*, java.io.*"
 %>
<%!
public class xparser 
{
    String displayStrings[] = new String[1000];
    int numberDisplayLines = 0;
    public String[] displayDocument(String uri) 
    {
        try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        try {
            db = dbf.newDocumentBuilder();
        } 
        catch (ParserConfigurationException pce) {}
        Document document = null;
        document = db.parse(uri);
        numberDisplayLines = 0;
        display(document, "");
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
        return displayStrings;
    } 
    public void display(Node node, String indent) 
    {
        if (node == null) {
            return;
        }
        int type = node.getNodeType();
        switch (type) {
            case Node.DOCUMENT_NODE: {
                displayStrings[numberDisplayLines] = indent;
                displayStrings[numberDisplayLines] += "<?xml version=\"1.0\" encoding=\""+
                  "UTF-8" + "\"?>";
                numberDisplayLines++;
                display(((Document)node).getDocumentElement(), "");
                break;
             }
             case Node.ELEMENT_NODE: {
                 displayStrings[numberDisplayLines] = indent;
                 displayStrings[numberDisplayLines] += "<";
                 displayStrings[numberDisplayLines] += node.getNodeName();
                 int length = (node.getAttributes() != null) ? node.getAttributes().getLength() : 0;
                 Attr attributes[] = new Attr[length];
                 for (int i = 0; i < length; i++) {
                     attributes[i] = (Attr)node.getAttributes().item(i);
                 }
                 for (int i = 0; i < attributes.length; i++) {
                     Attr attribute = attributes[i];
                     displayStrings[numberDisplayLines] += " ";
                     displayStrings[numberDisplayLines] += attribute.getNodeName();
                     displayStrings[numberDisplayLines] += "=\"";
                     displayStrings[numberDisplayLines] += attribute.getNodeValue();
                     displayStrings[numberDisplayLines] += "\"";
                 }
                 displayStrings[numberDisplayLines] += ">";
                 numberDisplayLines++;
                 NodeList childNodes = node.getChildNodes();
                 if (childNodes != null) {
                     length = childNodes.getLength();
                     indent += "    ";   
                     for (int i = 0; i < length; i++ ) {
                        display(childNodes.item(i), indent);
                     }
                 }
                 break;
             }
             case Node.CDATA_SECTION_NODE: {
                 displayStrings[numberDisplayLines] = indent;
                 displayStrings[numberDisplayLines] += "<![CDATA[";
                 displayStrings[numberDisplayLines] += node.getNodeValue();
                 displayStrings[numberDisplayLines] += "]]>";
                 numberDisplayLines++;
                 break;
             }
             case Node.TEXT_NODE: {
                 displayStrings[numberDisplayLines] = indent;
                 String newText = node.getNodeValue().trim();
                 if(newText.indexOf("\n") < 0 && newText.length() > 0) {
                     displayStrings[numberDisplayLines] += newText;
                     numberDisplayLines++;
                 }
                 break;
             }
             case Node.PROCESSING_INSTRUCTION_NODE: {
                 displayStrings[numberDisplayLines] = indent;
                 displayStrings[numberDisplayLines] += "<?";
                 displayStrings[numberDisplayLines] += node.getNodeName();
                 String text = node.getNodeValue();
                 if (text != null && text.length() > 0) {
                     displayStrings[numberDisplayLines] += text;
                 }
                 displayStrings[numberDisplayLines] += "?>";
                 numberDisplayLines++;
                 break;
            }    
        }
        if (type == Node.ELEMENT_NODE) {
            displayStrings[numberDisplayLines] = indent.substring(0, indent.length() - 24);
            displayStrings[numberDisplayLines] += "</";
            displayStrings[numberDisplayLines] += node.getNodeName();
            displayStrings[numberDisplayLines] += ">";
            numberDisplayLines++;
            indent += "    ";
        }
    } 
}
%>

  
    Parsing an XML Document With DOM
  
  
    

Parsing an XML Document With DOM


    <% xparser p = new xparser(); 
        String[] s = p.displayDocument(application.getRealPath("/") + "test.xml");
 
        for(int i = 0; i < s.length; i++){
            if(s[i] != null){
    %>
            <%= s[i] %>

    <%
            }
        }
    %>
    

XML demo file