Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

Java XML problems

Hello,
I am writing an app that loads an XML document as its config file. I keep gettin the following error when I run the program, and test the loading of XML. org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'root'.
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:172)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:382)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1944)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:705)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:330)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(XMLNSDocumentScannerImpl.java:779)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1794)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:250)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292)
        at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:172)
        at config.loadXMLConfig(config.java:83)
        at config.<init>(config.java:41)
        at SMTP.main(SMTP.java:10)
I am new to XML in general, and have never attempted to code java xml before. I wrote most of this code based on the XML tutorials on sun's site. The correct fix is worth 500 points.

The XML Document:

<?xml version="1.0"?>
<root>
<!-- start of MailPro-LM Configuration -->
      <!-- basic system configuration options -->
      <basic>
            <system_mode>distributor</system_mode>
            <max_smtp_out>25</max_smtp_out>
            <max_retries>3</max_retries>
            <retry_after>10</retry_after>
            <log_path>/root/log.txt</log_path>
            <accounting_type>database</accounting_type>
            <accounting_path>/root/acct.txt</accounting_path>
            <spool_path>/root/spool/</spool_path>
            <mailer_id>1</mailer_id>
            <dns_max_retries>3</dns_max_retries>
            <mta_ip>192.168.1.115</mta_ip>                                                                                                      
      </basic>
</root>


My Code so far:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.FactoryConfigurationError;  
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;  
import org.xml.sax.SAXParseException;
import java.io.File;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;

public class config{
         private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
       private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
       private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
      
      //XML Data Container
      static Document cfg;
      
        public config()
        {
               loadXMLConfig();
        }

  public boolean loadXMLConfig()
      {
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          factory.setValidating(true);
        factory.setNamespaceAware(true);
        factory.setNamespaceAware(true);
        factory.setValidating(true);

        factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);

            try
            {
                  DocumentBuilder builder = factory.newDocumentBuilder();
                  builder.setErrorHandler(
                                new org.xml.sax.ErrorHandler() {
                                    // ignore fatal errors (an exception is guaranteed)
                                    public void fatalError(SAXParseException exception)
                                    throws SAXException {
                                    }

                                    // treat validation errors as fatal
                                    public void error(SAXParseException e)
                                    throws SAXParseException
                                    {
                                      throw e;
                                    }

                                    // dump warnings too
                                    public void warning(SAXParseException err)
                                    throws SAXParseException
                                    {
                                      System.out.println("** Warning"
                                         + ", line " + err.getLineNumber()
                                         + ", uri " + err.getSystemId());
                                      System.out.println("   " + err.getMessage());
                                    }
                                }
                              );
                  cfg = builder.parse(new File("config.xml"));
                  
          }
            catch (SAXException sxe)
            {
            // Error generated during parsing
            Exception  x = sxe;
            if (sxe.getException() != null)
                x = sxe.getException();
            x.printStackTrace();
            } catch (ParserConfigurationException pce) {
             // Parser with specified options can't be built
             pce.printStackTrace();
          } catch (IOException ioe) {
             // I/O error
             ioe.printStackTrace();
          }
          
          return true;
      }
}

0
richardsimnett
Asked:
richardsimnett
  • 2
1 Solution
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
http://www.java2s.com/Code/Java/XML/SAX.htm

i am not sure weather we can directly help you in correcting the program so only i am providing you the link which will help you to solve it.

Thank You.
0
 
CEHJCommented:
Get rid of

>>
         factory.setValidating(true);
        factory.setNamespaceAware(true);
        factory.setNamespaceAware(true);
        factory.setValidating(true);

        factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
>>

as there's nothing to validate (you have no schema or DTD)
0
 
CEHJCommented:
:-)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now