[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

SAXParseException: Document is invalid: no grammar found

Hi,

The following when called with a validated XML file as a parameter produces the above Exception, and I can't figure out why, any ideas?

Cheers,
Steve



XMLDOM.java =============


import ...

public class XMLDOM extends JPanel {

      static Document document;


      static final int windowHeight = 460;
      static final int leftWidth = 300;
      static final int rightWidth = 340;
    static final int windowWidth = leftWidth + rightWidth;


      public XMLDOM() {

            // Set up the tree
            JTree tree = new JTree();

            // Build left-side view
            JScrollPane treeView = new JScrollPane(tree);
            treeView.setPreferredSize(new Dimension( leftWidth, windowHeight ));

            // Build right-side view
            JEditorPane htmlPane = new JEditorPane("text/html","");
            htmlPane.setEditable(false);
            JScrollPane htmlView = new JScrollPane(htmlPane);
            htmlView.setPreferredSize(new Dimension( rightWidth, windowHeight ));


            // Build split-pane view
            JSplitPane splitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, treeView, htmlView );
            splitPane.setContinuousLayout( true );
            splitPane.setDividerLocation( leftWidth );
            splitPane.setPreferredSize(
            new Dimension( windowWidth + 10, windowHeight+10 ));

            // Add GUI components
            this.setLayout(new BorderLayout());
            this.add("Center", splitPane );





      } // Constructor


      public static void main(String  [] argv) {

            if (argv.length != 1) {
                  System.err.println("Usage: java XMLDOM filename");
                  System.exit(1);
            }

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            // set validation awareness

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




            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());
                              }
                          }
                  );


                                      /* doesn't seem to solve
                  SAXParserFactory saxFactory = SAXParserFactory.newInstance();
                  SAXParser saxParser = saxFactory.newSAXParser();
                  org.xml.sax.XMLReader xmlReader = saxParser.getXMLReader();
                  xmlReader.setFeature("http://apache.org/xml/features/validation/schema", true);
                  */

                  document = builder.parse(new File(argv[0]));

                  makeFrame();



            } catch (SAXException e) {
                  // Error generated during parsing
                  Exception x = e;
                  if (e.getException() != null) {
                        x = e.getException();
                  }
                  x.printStackTrace();

            } catch (ParserConfigurationException e) {
                  // Parser with specified options can't be built
                  e.printStackTrace();
            } catch (IOException e) {
                  // I/O Error
                  e.printStackTrace();
            }


      } // end of main

      public static void makeFrame() {
          // Set up a GUI framework
          JFrame frame = new JFrame("DOM Test");
          frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
          });

          // Set up the tree, the views, and display it all
          final XMLDOM domPanel = new XMLDOM();
          frame.getContentPane().add("Center", domPanel );
          frame.pack();
          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
          int w = windowWidth + 10;
          int h = windowHeight + 10;
          frame.setLocation(screenSize.width/3 - w/2, screenSize.height/2 - h/2);
          frame.setSize(w, h);
          frame.setVisible(true);
      } // makeFrame


}
0
steve_bagnall
Asked:
steve_bagnall
  • 14
  • 10
  • 6
  • +1
1 Solution
 
CEHJCommented:
The error may well be in the xml file. Can you post it?
0
 
steve_bagnallAuthor Commented:
No problem ...


<?xml version='1.0' encoding='utf-8'?>

<choices
     xmlns="http://www.churchillchina.com/gapanalysis"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.churchillchina.com/gapanalysis test.xsd">
         
     <choice class="retail" function="search">
          <options name="type">
               <option name="MASS MARKETS">
                    <options name="category">
                         <option name="DISCOUNT" />
                         <option name="VOLUME" />
                         <option name="OTHERS" />
                    </options>
               </option>
               <option name="MIDDLE MARKETS">
                    <options name="category">
                         <option name="MIDDLE MARKETS" />
                    </options>
               </option>
          </options>
     </choice>
     
     <choice class="retail" function="data">
          <options name="type">
               <option name="MASS MARKETS">
                    <options name="cat">
                         <option name="DISCOUNT" />
                         <option name="VOLUME" />
                         <option name="OTHERS" />
                    </options>
               </option>
               <option name="MIDDLE MARKETS">
                    <options name="category">
                         <option name="MIDDLE MARKETS" />
                    </options>
               </option>
          </options>
     </choice>

     <choice class="product" function="search">
          <options name="product group">
               
               <option name="MUGS">
                    <options name="body type">
                         <option name="SPECIFIC" />
                         <option name="BONE CHINA" />
                         <option name="PORCELAIN" />
                         <option name="STONEWARE" />
                         <option name="EARTHENWARE" />
                         <option name="FINE CHINA" />
                         <option name="GLASS" />
                         <option name="OTHER" />
                    </options>
                    <options name="size">
                         <option name="7 OZ OR LESS" />
                         <option name="8 OZ" />
                         <option name="9 OZ" />
                         <option name="10 OZ" />
                         <option name="12 OZ" />
                         <option name="14 OZ" />
                         <option name="16 OZ" />
                         <option name="18 OZ" />
                         <option name="20 OZ" />
                         <option name="20 OZ +" />
                    </options>
                    <options name="shape">
                         <option name="TRADITIONAL M/C MADE" />
                         <option name="TRADITIONAL CAST" />
                         <option name="TRADITIONAL OTHER" />
                         <option name="MODERN M/C MADE" />
                         <option name="MODERN CAST" />
                         <option name="MODERN OTHER" />
                    </options>
                    <options name="design category">
                         <option name="CLASSIC">
                              <options name="design">
                                   <option name="ART DECO" />
                                   <option name="NOUVEAU" />
                                   <option name="WILLIAM MORRIS" />
                                   <option name="TOILE" />
                                   <option name="PAISLEY" />
                              </options>
                         </option>
                         <option name="MODERN CLASSIC" />
                         <option name="TRADITIONAL">
                              <options name="design">
                                   <option name="FRUIT" />
                                   <option name="FLORAL" />
                                   <option name="THEMED (NAUTICAL/COCKEREL)" />
                                   <option name="COUNTRY" />    
                              </options>
                         </option>
                         <option name="CONTEMPERARY">
                              <options name="design">
                                   <option name="FLORAL" />
                                   <option name="GEOMETRIC" />    
                              </options>
                         </option>
                         <option name="NATURAL (RETREAT)">
                              <options name="design">
                                   <option name="ORGANIC" />
                              </options>
                         </option>
                         <option name="URBAN">
                              <options name="design">
                                   <option name="LOFT" />
                                   <option name="RETRO" />
                              </options>
                         </option>
                         <option name="GLOBAL VILLAGE (FUSION)">
                              <options name="design">
                                   <option name="ETHNIC" />
                                   <option name="MORROCO" />
                                   <option name="INDIAN" />
                                   <option name="JAPANESE" />
                              </options>
                         </option>
                         <option name="MONO">
                              <options name="design">
                                   <option name="BLACK" />
                                   <option name="RED" />
                              </options>
                         </option>
                         <option name="BOUDOIR/ROMANCE" />
                         <option name="FUN/FUNKY">
                              <options name="design">
                                   <option name="CATS/DOGS" />
                                   <option name="FARMYARD (PIG/COW/SHEEP)" />
                                   <option name="CHARACTER" />
                              </options>
                         </option>
                         <option name="SIMPLICITY (MINIMALIST)">
                              <options name="design">
                                   <option name="WHITE" />
                              </options>
                         </option>
                         <option name="SEASONS">
                              <options name="design">
                                   <option name="EASTER" />
                                   <option name="CHRISTMAS" />
                                   <option name="HALIOWEEN" />
                                   <option name="BACK TO SCHOOL" />
                              </options>
                         </option>
                         <option name="COLOUR">
                              <options name="design">
                                   <option name="THEMES" />
                              </options>
                         </option>
                    </options>
                   
                    <options name="brand">
                         <option name="DUNOON" />
                         <option name="HUDSON &amp; MIDDLETON" />
                         <option name="ROY KIRKHAM" />
                         <option name="TTC" />
                         <option name="GLENEAGLES" />
                         <option name="RAYWARE" />
                         <option name="RANSAAT" />
                         <option name="PREMIER HOUSEWARES" />
                         <option name="EMS (TRADEWINDS, WATERSIDE)" />
                         <option name="JUST MUGS" />
                         <option name="TAMS" />
                    </options>
                   
                    <options name="packaging">
                         <option name="LOOSE" />
                         <option name="INDIVIDUAL BOX" />
                         <option name="MULTI PACK 2" />
                         <option name="MULTI PACK 3" />
                         <option name="MULTI PACK 4" />
                         <option name="MULTI PACK 6" />
                         <option name="MULTI PACK 8" />
                         <option name="MULTI PACK 12" />
                         <option name="OTHER" />
                    </options>
               </option>
          </options>
     </choice>

     <choice class="product" function="data">
          <options name="productgroup">

               <option name="MUGS">
                    <options name="body type">
                         <option name="SPECIFIC" />
                         <option name="BONE CHINA" />
                         <option name="PORCELAIN" />
                         <option name="STONEWARE" />
                         <option name="EARTHENWARE" />
                         <option name="FINE CHINA" />
                         <option name="GLASS" />
                         <option name="OTHER" />
                    </options>
                    <options name="size">
                         <option name="7 OZ OR LESS" />
                         <option name="8 OZ" />
                         <option name="9 OZ" />
                         <option name="10 OZ" />
                         <option name="12 OZ" />
                         <option name="14 OZ" />
                         <option name="16 OZ" />
                         <option name="18 OZ" />
                         <option name="20 OZ" />
                         <option name="20 OZ +" />
                    </options>
                    <options name="shape">
                         <option name="TRADITIONAL M/C MADE" />
                         <option name="TRADITIONAL CAST" />
                         <option name="TRADITIONAL OTHER" />
                         <option name="MODERN M/C MADE" />
                         <option name="MODERN CAST" />
                         <option name="MODERN OTHER" />
                    </options>
                    <options name="design category">
                         <option name="CLASSIC">
                              <options name="design">
                                   <option name="ART DECO" />
                                   <option name="NOUVEAU" />
                                   <option name="WILLIAM MORRIS" />
                                   <option name="TOILE" />
                                   <option name="PAISLEY" />
                              </options>
                         </option>
                         <option name="MODERN CLASSIC" />
                         <option name="TRADITIONAL">
                              <options name="design">
                                   <option name="FRUIT" />
                                   <option name="FLORAL" />
                                   <option name="THEMED (NAUTICAL/COCKEREL)" />
                                   <option name="COUNTRY" />    
                              </options>
                         </option>
                         <option name="CONTEMPERARY">
                              <options name="design">
                                   <option name="FLORAL" />
                                   <option name="GEOMETRIC" />    
                              </options>
                         </option>
                         <option name="NATURAL (RETREAT)">
                              <options name="design">
                                   <option name="ORGANIC" />
                              </options>
                         </option>
                         <option name="URBAN">
                              <options name="design">
                                   <option name="LOFT" />
                                   <option name="RETRO" />
                              </options>
                         </option>
                         <option name="GLOBAL VILLAGE (FUSION)">
                              <options name="design">
                                   <option name="ETHNIC" />
                                   <option name="MORROCO" />
                                   <option name="INDIAN" />
                                   <option name="JAPANESE" />
                              </options>
                         </option>
                         <option name="MONO">
                              <options name="design">
                                   <option name="BLACK" />
                                   <option name="RED" />
                              </options>
                         </option>
                         <option name="BOUDOIR/ROMANCE" />
                         <option name="FUN/FUNKY">
                              <options name="design">
                                   <option name="CATS/DOGS" />
                                   <option name="FARMYARD (PIG/COW/SHEEP)" />
                                   <option name="CHARACTER" />
                              </options>
                         </option>
                         <option name="SIMPLICITY (MINIMALIST)">
                              <options name="design">
                                   <option name="WHITE" />
                              </options>
                         </option>
                         <option name="SEASONS">
                              <options name="design">
                                   <option name="EASTER" />
                                   <option name="CHRISTMAS" />
                                   <option name="HALIOWEEN" />
                                   <option name="BACK TO SCHOOL" />
                              </options>
                         </option>
                         <option name="COLOUR">
                              <options name="design">
                                   <option name="THEMES" />
                              </options>
                         </option>
                    </options>

                    <options name="brand">
                         <option name="DUNOON" />
                         <option name="HUDSON &amp; MIDDLETON" />
                         <option name="ROY KIRKHAM" />
                         <option name="TTC" />
                         <option name="GLENEAGLES" />
                         <option name="RAYWARE" />
                         <option name="RANSAAT" />
                         <option name="PREMIER HOUSEWARES" />
                         <option name="EMS (TRADEWINDS, WATERSIDE)" />
                         <option name="JUST MUGS" />
                         <option name="TAMS" />
                    </options>

                    <options name="packaging">
                         <option name="LOOSE" />
                         <option name="INDIVIDUAL BOX" />
                         <option name="MULTI PACK 2" />
                         <option name="MULTI PACK 3" />
                         <option name="MULTI PACK 4" />
                         <option name="MULTI PACK 6" />
                         <option name="MULTI PACK 8" />
                         <option name="MULTI PACK 12" />
                         <option name="OTHER" />
                    </options>
               </option>

          </options>
     </choice>
                   
     <choice class="prodinret" function="search">
          <options name="acorn">
               <option name="A: THRIVING" />
               <option name="B: EXPANDING" />
               <option name="C: RISING" />
               <option name="D: SETTLING" />
               <option name="E: ASPIRING" />
               <option name="F: STRIVING" />
          </options>
         
          <options name="price">
               <option name="&lt;See PriceRanges.java&gt;" />
          </options>
     </choice>
     
     <choice class="prodinret" function="data">
          <options name="acorn">
               <option name="A: THRIVING" />
               <option name="B: EXPANDING" />
               <option name="C: RISING" />
               <option name="D: SETTLING" />
               <option name="E: ASPIRING" />
               <option name="F: STRIVING" />
          </options>

          <options name="price">
               <option name="&lt;See PriceRanges.java&gt;" />
          </options>
     </choice>
     
</choices>
0
 
CEHJCommented:
Are those urls in the file resolvable?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
steve_bagnallAuthor Commented:
No they are just unique identifiers, do they have to be resolvable to IP?
0
 
CEHJCommented:
>>do they have to be resolvable to IP?

Yes, if you're validating. Try setting validation off
0
 
steve_bagnallAuthor Commented:
It does work with these commented out ...

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

but I want the validation.  I always thought that the urls in namespace names in XML were just used to ensure uniqueness?  If I go to this site and enter the XML and XSD files they validate fine ...

http://apps.gotdotnet.com/xmltools/xsdvalidator/Default.aspx

What do you think I need to do to get this to work with validation turned on?

0
 
CEHJCommented:
>>What do you think I need to do to get this to work with validation turned on?

You need to either make those entities at those urls reachable at that address or make them locally reachable
0
 
CEHJCommented:
>>
 I always thought that the urls in namespace names in XML were just used to ensure uniqueness?  If I go to this site and enter the XML and XSD files they validate fine ...
>>

You might be able to get away with non-resolution of the namespaces, but not of the schema i don't think. On the site they probably resolve the urls
0
 
steve_bagnallAuthor Commented:
Sorry to appear thick, I can't make them reachable at www.churchillchina.com. becasue that's a Domino web server.  Are you saying that I need a web server running and the XML and XSD files need to be in the gapanalysis virtual drive of that server?
0
 
CEHJCommented:
Whatever entity is parsing the file needs to be able to read the urls for complete validation. That will mean effectively that you'll need to serve them. A web server is an obvious option, yes.
0
 
steve_bagnallAuthor Commented:
The form of the schemaLocation attribute in XML is ...

schemaLocation="URL file.xsd"

where

URL is the name of the namespace of the schema document you are about to give
and
file.xsd is the location of the schema file.

The name of the namespace for both the XML and XSD files is www.churchillchina.com/gapanalysis

My java, xml and schema files are all in the same directory.

XMLDOM.java is defo finding the xml file, as if I change the name of the xml file I get a different (FileNotFound) exception.

I've just checked and the line causing the problem is:

factory.setValidating(true);

leaving in the line

factory.setNamespaceAware(true);

works fine.  I don't mind trying the web server thing as a last resort, but I hope the solution lies elsewhere because that would be such a pain!
0
 
CEHJCommented:
>>
factory.setValidating(true);

leaving in the line

factory.setNamespaceAware(true);

works fine.
>>

That seems to bear out exactly what i said above:

>>You might be able to get away with non-resolution of the namespaces, but not of the schema i don't think.

The schema is required for validation

0
 
steve_bagnallAuthor Commented:
OK home time here .. think I'll sleep on it!  Thanks for help so far :)
0
 
CEHJCommented:
OK ;-). My above remarks are based on assuming that you know that the schema file must be readable in order to validate the xml. That's why *that* url is crucial
0
 
mightyoneCommented:
as CEHJ said validation needs an existing object to validate against, you nay take either shema or dtd. using dtd you may use inline or external but it must exist....

nevertheless even if setting validation (false) a non wellformedness is recognized..

also see

www.cafeconleche.org
0
 
aozarovCommented:
In java 1.5 you can do the following:
//factory.setValidating(true);
factory.setNamespaceAware(true);
javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(new File(test.xsd));
factory.setSchema(schema); // set the

** Note that I comment out the setValidation as this will trigger the old style of validating (using jaxp attributes)
When you set a Schema on the DocumentBuilderFactory, as I do above, the xml will be validated against it.
0
 
aozarovCommented:
>> The above test.xsd should be changed to "test.xsd" (forgot the "") when replaced my filename with yours..

If you want to use the old style of validating (using jaxp attributes) then you can do:

factory.setValidating(true); // here it is needed
factory.setNamespaceAware(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");  
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "test.xsd");
0
 
aozarovCommented:
There is a third option (actually I think this one you were trying to follow).
This one will use both JAXP properties and an external reference to your schema (defined inside your XML)
For that just do (comment out the last line as it will override your schema reference in the XML):
factory.setValidating(true); // here it is needed
factory.setNamespaceAware(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");  
//factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "test.xsd");  // comment this line if you want to use the reference defined in the XML using xsi:schemaLocation="name-space-uir xsd_path_relative_to_xml_or_full_path_using_file://"
e.g.
 xsi:schemaLocation="http://www.churchillchina.com/gapanalysis test.xsd" (which is what you did)
or
 xsi:schemaLocation="http://www.churchillchina.com/gapanalysis file:/c:/path_to_xsd_folder/test.xsd"


So basically what you were missing is: factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");  
0
 
steve_bagnallAuthor Commented:
That's done it, but it seems not to like my XSD file anymore; saying stuff like ...

"a derived type and that of it's base must both be mixed or both be element-only.  Type 'choices-type' is element only, but it's base type is not."

I can probably understand the message, but how come I can validate the XML and XSD on

http://apps.gotdotnet.com/xmltools/xsdvalidator/Default.aspx

but not here?
0
 
CEHJCommented:
>>xsi:schemaLocation="http://www.churchillchina.com/gapanalysis file:/c:/path_to_xsd_folder/test.xsd"

IOW just what i said to you earlier:

>>You need to either make those entities at those urls reachable at that address or make them locally reachable

??
0
 
steve_bagnallAuthor Commented:
test.xsd is reachable, it's in the same directory as both the XML file and the Java file.  The problems now seem to be to do with the recursive nature of my elements, and the naming conflicts that produces.  Anyway, more points are available on that score here ...

http://www.experts-exchange.com/Web/Web_Languages/XML/Q_21421608.html

Cheers,
Steve
0
 
CEHJCommented:
Since you seem to value the advice i give so little, i shan't be answering it
0
 
steve_bagnallAuthor Commented:
CEHJ, it's not that I don't value your advice, I do: you've helped me many times before.  It's just that I have the XSD file well and truly pinned down under house-arrest!  I get the same error even if I swap test.xsd for file:/c:/path_to_xsd_folder/test.xsd.  

Go-on answer my question :)
0
 
CEHJCommented:
I'm confused - if your question is not answered, then why have you accepted an answer?
0
 
steve_bagnallAuthor Commented:
Well, the question is answered, as in it's now validating the XML using the XSD file.  However, it seems that even though the XML validates OK at the web site with the link I've posted above, SAX apache xerces (not sure about the correct terminoligy here) seems to not like the format of my XSD file giving a "Unique Particle Attribution" error.  I think this is something to do with the fact that I am allowing an <option> element to contain a <options> element, which has to contain an <option> element.  The recursion is apparently causing a naming conflict or something?
0
 
CEHJCommented:
>> as in it's now validating the XML using the XSD file

That's because you are now using the suggestion i gave you earlier of making the schema locally resolvable, but since you've given me no credit on this question, the person who seems to have got all the credit can answer your other question ;-)
0
 
aozarovCommented:
>>That's because you are now using the suggestion i gave you earlier of making the schema locally resolvable
No, that is not correct. If you read fully the accepted answer you will realize that the problem WAS NOT the location of the XSD but rather that he didn't have this definition:
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");  
I just gave him more settings options as a general guidance.

>>  the person who seems to have got all the credit can answer your other question ;-)
And I will do that.
If you want the points then take them I don't care about them but don’t apply this scare tactics on the asker.
You and objects are really making the experience of using this site very unpleasant and I am seriously considering stopping it.
0
 
CEHJCommented:
>> but don’t apply this scare tactics on the asker

LOL. What would i be 'scaring' the asker *about*?!
0
 
aozarovCommented:
>> LOL. What would i be 'scaring' the asker *about*?!
"Since you seem to value the advice i give so little, i shan't be answering it"
Suggesting that you will not answer his questions anymore...
0
 
CEHJCommented:
>>Suggesting that you will not answer his questions anymore...

I meant the particular question referred to in fact. The conclusion you jumped to is your own
0
 
aozarovCommented:
>> I meant the particular question referred to in fact. The conclusion you jumped to is your own
You gave no such suggestion.
In any case I thank you and objects to for the lovely experience I had using this site and I wish you all the very best in your pursuit for the valuable E.E points.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 14
  • 10
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now