2

I'm trying to read an xml file from jsp file. they are both under the same directory and I'm using BufferReader to read from the xml file. i'm getting the error "error on line 3 at column 6: xml declaration allowed only at the start of the document ". search the web for the reason and all I got was that the xml file should not start with a new line , but I've checked my xml deleting every space at the beginning. this is the only xml file in my program so I dont realy know what could be the problem. I'm adding my xml file and my jsp file.. I'm probebly doing something wrong but I just dont know what.

my xml file :

    <?xml version="1.0"encoding="UTF-8"?>
       <quiz>
         <question type="multichoice"  >
       <name>
    <text>Name of question</text>
      </name>
     <questiontext format="html">
    <text>What is the answer to this question?</text>
     </questiontext>
     <answer fraction="100">
    <text>The correct answer</text>
    <feedback><text>Correct!</text></feedback>
     </answer>
     <answer fraction="0">
     <text>A distractor</text>
     <feedback><text>Ooops!</text></feedback>
     </answer>
         <answer fraction="0">
             <text>Another distractor</text>
             <feedback><text>Ooops!</text></feedback>
         </answer>
         <shuffleanswers>1</shuffleanswers>
         <single>true</single>
         <answernumbering>abc</answernumbering>
         </question>
            <question type="truefalse">
        <name>
              <text>Name of question</text>
            </name>
            <questiontext format="html">
            <text>What is the answer to this question?</text>
            </questiontext>
            <answer fraction="100">
               <text>true</text>
               <feedback><text>Correct!</text></feedback>
            </answer>
            <answer fraction="0">
               <text>false</text>
               <feedback><text>Ooops!</text></feedback>
            </answer>
          </question>
          <question type="numerical">
        <name>
               <text>Name of question</text>
            </name>
            <questiontext format="html">
              <text>What is the answer to this question?</text>
            </questiontext>
            <answer fraction="100">
              <text>23</text>
              <feedback><text>Feedback</text></feedback>
            </answer>
         </question>
    </quiz>    

there is no white space before the definition of xml ( first line in file). and my jsp file :

    <%@ page contentType="text/xml" %>
    <%@ page import="java.io.*" %>    
    <%
    //dump out the file
    BufferedReader in = new BufferedReader(new FileReader("questions.xml"));
    String line;
    while((line = in.readLine())!=null){
    out.print(line);
    }
    in.close();
    %>    

I'm new in programming in html using java so it will be great if you can help me.. thanx

4

5 に答える 5

4

I presume you're seeing that error in the browser when you navigate to the JSP page. The problem is in the page directives at the top of the JSP

<%@ page contentType="text/xml" %>
<%@ page import="java.io.*" %>

Since each of these is followed by a newline you'll end up with two newlines written to out before the data (written by the out.println statements), which is why the error message says you have an XML declaration on line 3.

Removing the newlines, or moving them inside the directives rather than between them should fix this problem:

<%@ page contentType="text/xml"
%><%@ page import="java.io.*"
%><%
BufferedReader ....

As an aside, for reading the file you should probably use a FileInputStream (or better application.getResourceAsStream) and an InputStreamReader explicitly set to the correct character encoding to match the XML file, rather than rely on FileReader which always uses the platform default encoding.

于 2013-03-09T19:31:47.317 に答える
2

You can add the following line at the beginning of your document:

<%@ page trimDirectiveWhitespaces="true" %>

This should resolve your issue.

于 2017-03-14T10:51:43.097 に答える
1

There may be problem with xml file, check if it contains Byte order mark, if yes remove it. You can use simple Notepad++ to remove it by editing and checking the formating UTF-8 without BOM

http://en.wikipedia.org/wiki/Byte_order_mark

于 2013-03-09T19:55:11.647 に答える
0

Change this:

<?xml version="1.0"encoding="UTF-8"?>

to

<?xml version="1.0" encoding="UTF-8"?>

You forgot to add space between "1.0" and encoding.

于 2013-03-09T16:15:37.943 に答える
0

Its a bug of php Storm If you using php storm , php storm Makes your code start from the second line (no matter what you do) ! So you should go to your host and edit your file by direct admin or cpanel editor and put your

 <?xml version=“1.0” encoding=“UTF-8” ?>

Code at the first line, “hope it helps”</p>

于 2021-06-22T19:32:14.307 に答える