how to use SAX

Discussions

EJB programming & troubleshooting: how to use SAX

  1. how to use SAX (1 messages)

    SAX need a handler to process events?
    but i don't know how to use SAX directly to parse a xml file.
    I wish to find a example code.(it's better if use JAXP provided vy SUN)

    thanks

    Threaded Messages (1)

  2. how to use SAX[ Go to top ]

    Hi jerry

    U gotta get the document node of the XML in a normalized form. then u can access each node and nodelist by seeing the api ..Attaching the code for normalising and getting the document node
    I have put the whole parser program in it
    ******************************************************

    import java.io.File;
    import org.w3c.dom.*;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import com.sun.xml.parser.Resolver;
    import com.sun.xml.tree.XmlDocument;
    import org.xml.sax.InputSource;
    import java.util.StringTokenizer;

    public class contextfinal1
    {

    //Declaring the constructor public for Bean Access
    public contextfinal1() {}

        //
        // Parsing an XML document stored in a file.
        //
        private static String contextfile = "contextfinal.xml";
        private static InputSource input;
        private static Document doc;
        private static Element root;
    int nodeidx;
        String [] propval = new String[5];
        NodeList [] mainNodeList = new NodeList[7];

    public String getProperty(String prop)
    {

    int i = 0;
            String property_value = "Not Found";
    StringTokenizer st = new StringTokenizer(prop , ".");
    while (st.hasMoreTokens()) {
             propval[i] = st.nextToken();
             i++;
          }

    mainNodeList = this.getNodeList();
    for (int count = 0;count < nodeidx ; count++)
    {

    NodeList selectedNodeList = mainNodeList[count];
    try {
    property_value = getVal(selectedNodeList , 1);
    if (!((property_value.equalsIgnoreCase("Not Found")) ||
    (property_value == null))) {
    return property_value;
    }
    } catch (Exception e) {}
    }
          return property_value;
    }

        public String getVal(NodeList ctx , int index)
        {
    try {
    for (int i = 1 ; i < ctx.getLength() ; i=i+2)
    {
    if (ctx.item(i).getNodeName().equals(propval[index]))
    {
    if (!(ctx.item(i).getChildNodes().getLength() == 1))
    {
    NodeList ctx1 = ctx.item(i).getChildNodes();
    return this.getVal(ctx1 , (index+1));
    } else {
    return ctx.item(i).getFirstChild().getNodeValue();
    }
    }
    }

    } catch (Exception e) {System.out.println(e.toString());}
    return "Not Found";
        }

    static { // static initializer same as constructor in non-static cases

            try {

         // turn the filename into an input source
    input = Resolver.createInputSource (new File (contextfile));

    // turn it into an in-memory object
    // ... the "false" flag says not to validate
      doc = XmlDocument.createXmlDocument (input, false);

    // normalize text representation
    doc.getDocumentElement ().normalize ();

    root = doc.getDocumentElement();

    } catch (Exception err) {
                System.out.println ("Parsing error " + err.toString());
            }

    }


    public NodeList[] getNodeList()
    {

    try {
    for (nodeidx=0;;nodeidx++){
    mainNodeList[nodeidx] = doc.getElementsByTagName(propval[0]).item(nodeidx).getChildNodes();
    }

    } catch (Exception e) {}
    return mainNodeList;
    }


        public static void main (String argv [])
        {
    contextfinal1 appl = new contextfinal1();
    System.out.println("\nProperty : context.database.wait_millis = " + appl.getProperty("context.database.wait_millis"));
         System.out.println("\nProperty : context.report.scale5.top_significant_gap_thold = " + appl.getProperty("context.report.scale5.top_significant_gap_thold"));
         System.out.println("\nProperty : messaging.smtp.from = " + appl.getProperty("messaging.smtp.from"));
         System.out.println("\nProperty : survey.open_item_sec = " + appl.getProperty("survey.open_item_second"));
    System.out.println("\nProperty : context.make.drivering = " + appl.getProperty("context.make.drivering"));
        }

    }
    *****************************************************

    Shiv