Discussions

XML & Web services: how to solve this problem in JAXP...........urgent

  1. The program is :
    import java.io.File;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.Writer;

    //JAXP
    import javax.xml.parsers.FactoryConfigurationError;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;

    //DOM
    import org.w3c.dom.Node;
    import org.w3c.dom.Element;
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.NamedNodeMap;

    import org.xml.sax.helpers.XMLFilterImpl;
    import org.xml.sax.helpers.ParserAdapter;
    import org.xml.sax.ErrorHandler;



    public class JaxpExample {

     private static void printNode(Node node, String indent) {

         switch (node.getNodeType()) {

             case Node.DOCUMENT_NODE:
                 System.out.println("<xml version=\"5.0\">\n");
                 // recurse on each child
                 NodeList nodes = node.getChildNodes();
                 if (nodes != null) {
                     for (int i=0; i<nodes.getLength(); i++) {
                         printNode(nodes.item(i), "");
                     }
                 }
                 break;
                 
             case Node.ELEMENT_NODE:
                 String name = node.getNodeName();
                 System.out.print(indent + "<" + name);
                 NamedNodeMap attributes = node.getAttributes();
                 for (int i=0; i<attributes.getLength(); i++) {
                     Node current = attributes.item(i);
                     System.out.print(
                         " " + current.getNodeName() +
                         "=\"" + current.getNodeValue() +
                         "\"");
                 }
                 System.out.print(">");
                 
                 // recurse on each child
                 NodeList children = node.getChildNodes();
                 if (children != null) {
                     for (int i=0; i<children.getLength(); i++) {
                         printNode(children.item(i), indent + " ");
                     }
                 }
                 
                 System.out.print("</" + name + ">");
                 break;

             case Node.TEXT_NODE:
                 System.out.print(node.getNodeValue());
                 break;
         }

     }

     public static void main(String[] args) {
      try {
        
         // Create File object from incoming file
         File xmlFile = new File("satvexample.xml");
       
         
             
         
         
         
         XMLFilterImpl xmlfltr = new XMLFilterImpl();
         
         ParserAdapter parserAdapter = new ParserAdapter();
         ErrorHandler errorHandler=parserAdapter.getErrorHandler();
         System.out.println(errorHandler);
         xmlfltr.setErrorHandler(errorHandler);
             // Get Document Builder Factory
             DocumentBuilderFactory factory =
                     DocumentBuilderFactory.newInstance();

             // Turn on validation, and turn off namespaces
             factory.setValidating(true);
             factory.setNamespaceAware(false);

             // Obtain a document builder object
             DocumentBuilder builder = factory.newDocumentBuilder();
            
             System.out.println();
       
             System.out.println("Object to Parse (File) : " + xmlFile);
             System.out.println("Parser Implementation : " + builder.getClass());
             System.out.println();

             // Parse the document
             Document doc = builder.parse(xmlFile);

             // Print the document from the DOM tree and feed it an initial
             // indentation of nothing
             printNode(doc, "");
             
             System.out.println("\n");

         } catch (ParserConfigurationException e) {
             System.out.println("The underlying parser does not support " +
                                "the requested features.");
             e.printStackTrace();
         } catch (FactoryConfigurationError e) {
             System.out.println("Error occurred obtaining Document Builder " +
                                "Factory.");
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

    }

    The exception i got is :
    org.xml.sax.SAXException: System property org.xml.sax.parser not specified
    at org.xml.sax.helpers.ParserAdapter.<init>(Unknown Source)
    at JaxpExample.main(JaxpExample.java:94)
    Could any one tell me the solution for it........pls

    Threaded Messages (1)

  2. Classpath[ Go to top ]

    Hi,

    Can you please check your class path?.

    Ravi