Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems getting JAXP to read an XML file 1

Status
Not open for further replies.

rdoor

Technical User
May 4, 2002
43
US
I've downloaded WSDP 1.2 and over SDK 1.4.0. While installing, the installer indicated that WSDP was supported for 1.4.1 or later. I went ahead and installed it anyway but keep when I try to read an xml document, I get messages from SaxParseException that "XML declaration may only begin entities" or "Fatal Error. The processing instruction target matching '[xX][mM][lL]' is not allowed" or "No root element found."

I went ahead and installed SDK 1.4.2 at that point, but it makes no difference. I reinstalled WSDP as well. I did only load the JAXP and JAXB portions of the WSDP, due to memory limitations on my computer.

I used XML example from the Java Web Services Tutorial at:
I used java code from the same website at:
I'm stumped.

Roy from Iowa
 
If I am correct the following is the XML file and the java program you are using. The XML file seems to be OK. Are you sure you are passing this file to your program?

The XML file
Code:
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> 
- <!--   A SAMPLE set of slides  
  --> 
- <slideshow title=&quot;Sample Slide Show&quot; date=&quot;Date of publication&quot; author=&quot;Yours Truly&quot;>
- <!--  TITLE SLIDE 
  --> 
- <slide type=&quot;all&quot;>
  <title>Wake up to WonderWidgets!</title> 
  </slide>
- <!--  OVERVIEW 
  --> 
- <slide type=&quot;all&quot;>
  <title>Overview</title> 
- <item>
  Why 
  <em>WonderWidgets</em> 
  are great 
  </item>
  <item /> 
- <item>
  Who 
  <em>buys</em> 
  WonderWidgets 
  </item>
  </slide>
  </slideshow>
==========================================
The java program
==========================================
Code:
/*
 * @(#)DomEcho01.java	1.9 98/11/10
 *
 * Copyright (c) 2002 Sun Microsystems, Inc.  All rights reserved.  U.S.
 *
 * Government Rights - Commercial software.  Government users are subject
 * to the Sun Microsystems, Inc. standard license agreement and
 * applicable provisions of the FAR and its supplements.  Use is subject
 * to license terms.
 *
 * This distribution may include materials developed by third parties.
 * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
 * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
 * other countries. 
 * 
 */

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 DomEcho01{
    // Global value so it can be ref'd by the tree-adapter
    static Document document; 

      public static void main(String argv[])
    {
        if (argv.length != 1) {
          System.err.println(&quot;Usage: java DomEcho filename&quot;);
          System.exit(1);
                }

        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        //factory.setValidating(true);   
        //factory.setNamespaceAware(true);
        try {
           DocumentBuilder builder = factory.newDocumentBuilder();
           document = builder.parse( new File(argv[0]) );

        } catch (SAXParseException spe) {
           // Error generated by the parser
           System.out.println(&quot;\n** Parsing error&quot;
              + &quot;, line &quot; + spe.getLineNumber()
              + &quot;, uri &quot; + spe.getSystemId());
           System.out.println(&quot;   &quot; + spe.getMessage() );

           // Use the contained exception, if any
           Exception  x = spe;
           if (spe.getException() != null)
               x = spe.getException();
           x.printStackTrace();
 
        } 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();
        }
    } // main

  

}
 
Yup, that's the stuff!

I took out the

if [argv.length !=1] {. . . . }


code and simply replaced the new File information with the location of my SlideSample.xml.

Roy from Iowa
 
I have just tested the code, and have no problem running it.
Like you I changed :
Code:
  //document = builder.parse( new File(argv[0]) );
to
Code:
  document = builder.parse( new File(&quot;slideSample01.xml&quot;) );
And added the following to print the document :
Code:
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

        NodeList nodeList = document.getChildNodes();
        for (int i=0; i<nodeList.getLength(); i++) {
          Node node = nodeList.item(i);
          System.out.println(&quot;Node = &quot; + node);
        }
So I must ask you again : Are you using the correct xml file ?
 
Thanks for your response! The first time I saved it, I copied and pasted to Notebook. When I saved it directly as XML, the program ran OK.

Another question. I am trying to build a simple program to help teachers keep track of curriculum.

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<Curriculum>
<SubjectArea>
<Low>
<PreK-K>PreK-K Low ScienceText</PreK-K>
<one-two>1-2 Low Science Text</one-two>. . .

When I tried to run this xml file using the digits <1-2>. . .</1-2> in the tags, I got a &quot;not well-formed&quot; error. When I changed it to the above <one-two>. . .</one-two> , it read it fine. How come digits fail in the tags?

Roy from Iowa
 
The &quot;1-2&quot; or the &quot;one-two&quot; is the &quot;element type name&quot; or the &quot;GI = General Identifier&quot; and this must be a &quot;Name&quot;, which according to spec &quot;2.3 Common Syntactic Constructs&quot;, must start with a letter, underscore, or colon.

So &quot;1-2&quot; is NOT valid.
&quot;A1-2&quot; is valid.
 
Very good! Thanks for your help!

Roy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top