1 2 3 Previous Next 33 Replies Latest reply: Aug 10, 2011 4:50 PM by 866712 RSS

    Validator Error  'xml:lang' to a(n) '

    866712
      I am using a Servlet to send same Same data over 15 times in a gap of 3 secods..

      Each instance will have to check the data with an XSD , I am using

      Validator validator = schema.newValidator() for that. Since I have a huge XML and huge XSD, each process take around 30 secs.

      Now I get an Error
      "src-resolve: Cannot resolve the name 'xml:lang' to a(n) 'attribute declaration' component."

      But this is only for few of those 15 times I am doing it. and its random, I am not sure why it would work once and not for few..
      I am not accessing any file for XSD to think if its locking issue , nor any database .. the XSD is in a string the Servlet receives a doPost Request of XML ..


      Can anyone suggest ? Please

      Thanks
      Sur
        • 1. Re: Validator Error  'xml:lang' to a(n) '
          maheshguruswamy
          863709 wrote:
          I am using a Servlet to send same Same data over 15 times in a gap of 3 secods..

          Each instance will have to check the data with an XSD , I am using

          Validator validator = schema.newValidator() for that. Since I have a huge XML and huge XSD, each process take around 30 secs.

          Now I get an Error
          "src-resolve: Cannot resolve the name 'xml:lang' to a(n) 'attribute declaration' component."

          But this is only for few of those 15 times I am doing it. and its random, I am not sure why it would work once and not for few..
          I am not accessing any file for XSD to think if its locking issue , nor any database .. the XSD is in a string the Servlet receives a doPost Request of XML ..


          Can anyone suggest ? Please

          Thanks
          Sur
          Sounds like a threading issue? Servlets are not thread safe. If each request takes 30 secs to finish, and you are sending requests at 3 seconds interval, its likely that another request is hitting the validation code at the same time. Can you post your validation code here?
          • 2. Re: Validator Error  'xml:lang' to a(n) '
            866712
            Thanks Mahesh, but shouldn't Servlets have each thread on their own for each instance, and I thought all the instance run independently and without and intervention with each other..

            Also, I am sending 15 requests all "within" 3 secs not with a gap of 3 secs.. I have to handle this kind of situation where multiple requests come at the same time.

            Here is the code.

            .......

            BufferedWriter bw =
            new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));

            while ((line = in.readLine()) != null) {
            xml = xml + line;

            }
            String xsd ="...........................";

            InputStream temp = new ByteArrayInputStream(xsd.getBytes());
            Source asdf = new StreamSource(temp);

            try {
            Schema schema = factory.newSchema(asdf);
            Validator validator = schema.newValidator();


            InputStream is = new ByteArrayInputStream(xml.getBytes());

            Source source = new StreamSource(is);

            validator.validate(source);

            bw.write(xml);


            } catch (SAXException ex) {

            out.println("<cXML>");
            out.println("<Status code=\"500\" text=\"Error Parsing File \"/>");
            out.println("</Response>");
            out.println("</cXML>");
            out.println(ex.getMessage());
            bw.flush();
            bw.close();
            in.close();
            out.close();
            return;
            }
            • 3. Re: Validator Error  'xml:lang' to a(n) '
              maheshguruswamy
              863709 wrote:
              Thanks Mahesh, but shouldn't Servlets have each thread on their own for each instance, and I thought all the instance run independently and without and intervention with each other..
              No. The web container creates multiple threads for multiple requests, but only a single instance of the servlet is resused (in most application servers), so for most practical purposes, consider servlets as thread unsafe. Unless of course, your servlet implements http://download.oracle.com/javaee/6/api/javax/servlet/SingleThreadModel.html
              Also, I am sending 15 requests all "within" 3 secs not with a gap of 3 secs.. I have to handle this kind of situation where multiple requests come at the same time.

              Here is the code.

              .......

              BufferedWriter bw =
              new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));

              while ((line = in.readLine()) != null) {
              xml = xml + line;

              }
              String xsd ="...........................";

              InputStream temp = new ByteArrayInputStream(xsd.getBytes());
              Source asdf = new StreamSource(temp);

              try {
              Schema schema = factory.newSchema(asdf);
              Validator validator = schema.newValidator();


              InputStream is = new ByteArrayInputStream(xml.getBytes());

              Source source = new StreamSource(is);

              validator.validate(source);

              bw.write(xml);


              } catch (SAXException ex) {

              out.println("<cXML>");
              out.println("<Status code=\"500\" text=\"Error Parsing File \"/>");
              out.println("</Response>");
              out.println("</cXML>");
              out.println(ex.getMessage());
              bw.flush();
              bw.close();
              in.close();
              out.close();
              return;
              }
              I haven't debugged through your code but at first glance the Validator class stands out as being thread unsafe. Maybe you can try synchronizing the entire method and see if your problem goes away. Also check if any instance variables are being used in the execution thread somewhere...those are usually the ones causing threading issues.
              • 4. Re: Validator Error  'xml:lang' to a(n) '
                866712
                Mahesh,

                I tried making the doPost method synchronized by saying

                public synchronized void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {

                There is no difference , I still get the same error.

                I tried putting the entire class as Single Threaded Model , it says , Singlethreadmodel Deprecated, but I still went ahead and uploaded it, and found no use,

                The results have been the same as before.

                sur.
                • 5. Re: Validator Error  'xml:lang' to a(n) '
                  maheshguruswamy
                  863709 wrote:
                  Mahesh,

                  I tried making the doPost method synchronized by saying

                  public synchronized void doPost(HttpServletRequest request,
                  HttpServletResponse response) throws ServletException,
                  IOException {

                  There is no difference , I still get the same error.

                  I tried putting the entire class as Single Threaded Model , it says , Singlethreadmodel Deprecated, but I still went ahead and uploaded it, and found no use,

                  The results have been the same as before.

                  sur.
                  I just noticed this...the BufferedWriter...you are closing it only when an exception happens. If you don't specifically call close() or flush() on it, the writer wont write the contents but rather retain in memory. You should close it in a finally block. Also close the Streams in the finally block and not the catch block.

                  Edited by: maheshguruswamy on Aug 9, 2011 9:47 AM
                  • 6. Re: Validator Error  'xml:lang' to a(n) '
                    DrClap
                    You have a variable named "factory" which isn't obviously a local variable. If it's an instance variable (or worse yet, a static variable) then that's a thread-unsafe feature of the class. XML components like parsers and factories shouldn't be shared between threads unless the documentation explicitly states they are thread-safe.

                    And if you have really large XML documents, then your strategy of reading the whole document into memory as a String and then converting that String back to bytes is horribly inefficient in terms of both memory usage and processing time. (Especially since the parser will convert the bytes back to chars again!)

                    It appears that you are getting the XML document from a Reader whose name is "in". So just pass that Reader to the XML parser directly. (Hint: there's a version of InputSource which accepts a Reader.)
                    • 7. Re: Validator Error  'xml:lang' to a(n) '
                      866712
                      DrClap wrote:
                      You have a variable named "factory" which isn't obviously a local variable. If it's an instance variable (or worse yet, a static variable) then that's a thread-unsafe feature of the class. XML components like parsers and factories shouldn't be shared between threads unless the documentation explicitly states they are thread-safe.
                      how can I do it, should I declare the Factory as Private in the class instead of declaring in doPost?

                      And if you have really large XML documents, then your strategy of reading the whole document into memory as a String and then converting that String back to bytes is horribly inefficient in terms of both memory usage and processing time. (Especially since the parser will convert the bytes back to chars again!)

                      It appears that you are getting the XML document from a Reader whose name is "in". So just pass that Reader to the XML parser directly. (Hint: there's a version of InputSource which accepts a Reader.)
                      I have taken care of this, Thanks :P
                      • 8. Re: Validator Error  'xml:lang' to a(n) '
                        maheshguruswamy
                        You need to pull all that functionality out of the servlet class and into a utility class/method of its own. I wonder if you have declared the factory as a instance variable in the servlet class? No wonder, synchronizing the doPost didn't work. Also synchronization of the doPost is not a good idea at all. Once you have everything isolated out of the servlet class, call your utility method in a synchronized block.
                        • 9. Re: Validator Error  'xml:lang' to a(n) '
                          DrClap
                          863709 wrote:
                          DrClap wrote:
                          You have a variable named "factory" which isn't obviously a local variable. If it's an instance variable (or worse yet, a static variable) then that's a thread-unsafe feature of the class. XML components like parsers and factories shouldn't be shared between threads unless the documentation explicitly states they are thread-safe.
                          how can I do it, should I declare the Factory as Private in the class instead of declaring in doPost?
                          No, it should be a local variable. You didn't post all the relevant code so it wasn't possible for us to tell whether it was one or not.
                          • 10. Re: Validator Error  'xml:lang' to a(n) '
                            866712
                            Here is the entire code, Sorry didnt know if the Factory declaration was so important.. All the code is here..

                            The XSD is string of 1000 lines, so I am not posting it..
                                public void init(ServletConfig config) throws ServletException {
                                    super.init(config);
                                }
                            
                                public synchronized void doPost(HttpServletRequest request,
                                                   HttpServletResponse response) throws ServletException,
                                                                                        IOException {
                            
                                    SchemaFactory factory =
                                        SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
                              
                            
                                    response.setContentType(CONTENT_TYPE);
                                    String xmlString;
                                    xmlString = "asdf";
                            
                                    BufferedReader in = request.getReader();
                                    PrintWriter out = response.getWriter();
                                    DateFormat dateFormat =
                                        new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
                                    DateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
                                    DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
                                    DateFormat dateFormat3 = new SimpleDateFormat("HH:mm:ssZ");
                            
                                    Calendar cal = Calendar.getInstance();
                                    //System.out.println(dateFormat.format(cal.getTime()));
                                    File file =
                                        new File("../../../Integration/" +
                                                 dateFormat.format(cal.getTime()) + ".xml");
                                    String line;
                                    BufferedWriter bw =
                                        new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
                                    String xsd = "";
                            
                             xsd =" ... 1000 lines of XSD ......";
                            
                                    String xml = "";
                            
                                    while ((line = in.readLine()) != null) {
                                        xml = xml + line;
                                    
                                    }
                            
                                    InputStream temp = new ByteArrayInputStream(xsd.getBytes());
                                    Source asdf = new StreamSource(temp);
                                   
                                    try {
                                        Schema schema = factory.newSchema(asdf);
                                        Validator validator = schema.newValidator();
                            
                            
                                        InputStream is = new ByteArrayInputStream(xml.getBytes());
                            
                                        Source source = new StreamSource(is);
                            
                                    
                            
                                        validator.validate(source);
                            
                                           bw.write(xml);
                            
                            
                                    } catch (SAXException ex) {
                                        System.out.println(" is not valid because ");
                                        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                                       
                                        out.println("<cXML><Response>");
                                        out.println("<Status code=\"500\" text=\"Error Parsing File \"/>");
                                        out.println("</Response>");
                                        out.println("</cXML>");
                                        out.println(ex.getMessage());
                                        bw.flush();
                                        bw.close();
                                        in.close();
                                        out.close();
                                        return;
                                    }
                            
                            
                                    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                                    out.println("< cXML>"); 
                                    out.println("<Response>");
                                    out.println("<Status code=\"200\" text=\"OK\"/>");
                                    out.println("</Response>");
                                    out.println("</cXML>");
                                    bw.flush();
                                    bw.close();
                                    in.close();
                                    out.close();
                            
                                } 
                            Edited by: EJP on 10/08/2011 09:49: {noformat}
                            {noformat} tags                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                            • 11. Re: Validator Error  'xml:lang' to a(n) '
                              maheshguruswamy
                              Did you try what i suggested in my previous post?
                              • 12. Re: Validator Error  'xml:lang' to a(n) '
                                866712
                                Yes Mahesh, I have changed it here is the code I have now..
                                    private static final String CONTENT_TYPE =
                                        "text/xml; charset=windows-1252";
                                    private static final String DOC_TYPE = null;
                                  
                                    public void init(ServletConfig config) throws ServletException {
                                        super.init(config);
                                    }
                                 
                                    public   void doPost(HttpServletRequest request,
                                                       HttpServletResponse response) throws ServletException,
                                                                                            IOException {
                                
                                        
                                        SchemaFactory  factory =
                                         SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
                                     response.setContentType(CONTENT_TYPE);
                                        String xmlString;
                                        xmlString = "asdf";
                                
                                        BufferedReader in = request.getReader();
                                      BufferedReader bbn = in;
                                        PrintWriter out = response.getWriter();
                                        DateFormat dateFormat =
                                            new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
                                        DateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
                                        DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
                                        DateFormat dateFormat3 = new SimpleDateFormat("HH:mm:ssZ");
                                
                                        Calendar cal = Calendar.getInstance();
                                        //System.out.println(dateFormat.format(cal.getTime()));
                                        File file =
                                            new File("../../../Integration/" +
                                                     dateFormat.format(cal.getTime()) + ".xml");
                                        String line;
                                        BufferedWriter bw =
                                            new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
                                        String xsd = "";
                                        String xml = "";
                                
                                        while ((line = in.readLine()) != null) {
                                            xml = xml + line;
                                            //    bw.write(line);
                                        }
                                
                                        InputStream temp = new ByteArrayInputStream(xsd.getBytes());
                                        Source asdf = new StreamSource(temp);
                                        
                                        try {
                                            synchronized(this)
                                                {
                                     String yes_no=     Validate_XML.validate(bbn,asdf);
                                           
                                            out.println(yes_no); }
                                          } catch (SAXException ex) {
                                            
                                            
                                          }
                                
                                        finally { 
                                            bw.flush();
                                        bw.close();
                                        in.close();
                                        out.close();
                                        }
                                      }
                                Here is the validate class code
                                    public static String validate(BufferedReader bbn, Source asdf )throws IOException,SAXException{
                                        try{
                                          SchemaFactory  factory =
                                           SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
                                          Schema schema = factory.newSchema(asdf);
                                          Validator validator = schema.newValidator();
                                          Source source =new StreamSource(bbn);
                                          
                                            validator.validate(source);
                                            return "200";
                                          } catch (SAXException ex) {
                                            return "500"+ex.getMessage();
                                          }
                                        }
                                I am getting either same Error as mentioned in my first post or "Premature End of File"

                                Error

                                Please suggest

                                Thanks

                                Edited by: EJP on 10/08/2011 11:15: changed {noformat}
                                {noformat} to {noformat}
                                {noformat}. Poster please note.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                • 13. Re: Validator Error  'xml:lang' to a(n) '
                                  maheshguruswamy
                                  863709 wrote:
                                  Yes Mahesh, I have changed it here is the code I have now..

                                  >
                                  private static final String CONTENT_TYPE =
                                  "text/xml; charset=windows-1252";
                                  private static final String DOC_TYPE = null;

                                  public void init(ServletConfig config) throws ServletException {
                                  super.init(config);
                                  }

                                  public void doPost(HttpServletRequest request,
                                  HttpServletResponse response) throws ServletException,
                                  IOException {


                                  SchemaFactory factory =
                                  SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
                                  response.setContentType(CONTENT_TYPE);
                                  String xmlString;
                                  xmlString = "asdf";

                                  BufferedReader in = request.getReader();
                                  BufferedReader bbn = in;
                                  PrintWriter out = response.getWriter();
                                  DateFormat dateFormat =
                                  new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
                                  DateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
                                  DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
                                  DateFormat dateFormat3 = new SimpleDateFormat("HH:mm:ssZ");

                                  Calendar cal = Calendar.getInstance();
                                  //System.out.println(dateFormat.format(cal.getTime()));
                                  File file =
                                  new File("../../../Integration/" +
                                  dateFormat.format(cal.getTime()) + ".xml");
                                  String line;
                                  BufferedWriter bw =
                                  new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
                                  String xsd = "";
                                  String xml = "";

                                  while ((line = in.readLine()) != null) {
                                  xml = xml + line;
                                  // bw.write(line);
                                  }

                                  InputStream temp = new ByteArrayInputStream(xsd.getBytes());
                                  Source asdf = new StreamSource(temp);

                                  try {
                                  synchronized(this)
                                  {
                                  String yes_no= Validate_XML.validate(bbn,asdf);

                                  out.println(yes_no); }
                                  } catch (SAXException ex) {


                                  }

                                  finally {
                                  bw.flush();
                                  bw.close();
                                  in.close();
                                  out.close();
                                  }
                                  }

                                  >

                                  Here is the validate class code

                                  >
                                  public static String validate(BufferedReader bbn, Source asdf )throws IOException,SAXException{
                                  try{
                                  SchemaFactory factory =
                                  SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
                                  Schema schema = factory.newSchema(asdf);
                                  Validator validator = schema.newValidator();
                                  Source source =new StreamSource(bbn);

                                  validator.validate(source);
                                  return "200";
                                  } catch (SAXException ex) {
                                  return "500"+ex.getMessage();
                                  }
                                  }
                                  >


                                  I am getting either same Error as mentioned in my first post or "Premature End of File"

                                  Error

                                  Please suggest

                                  Thanks
                                  Move everything related to your xsd validation (the BufferedWriter, Stream, Sources etc) inside the validate utility method. Your doPost should have only one line of code (the validate method invocation) inside the synchronized block.
                                  • 14. Re: Validator Error  'xml:lang' to a(n) '
                                    EJP
                                    String line;
                                    // ...
                                    String xml = "";
                                    // ...
                                    while ((line = in.readLine()) != null) {
                                    xml = xml + line;
                                    //    bw.write(line);
                                    }
                                    
                                    InputStream is = new ByteArrayInputStream(xml.getBytes());
                                    Source source = new StreamSource(is);
                                    validator.validate(source);
                                    Why are you doing all that mucking around with readLine() and String.getBytes() and ByteArrayInputStream and string appends when you could construct the StreamSource directly from the stream you are reading the lines from? You are just (a) wasting memory (b) wasting time (c) risking charset problems (d) writing obscure code that someone else will have to understand and maintain and therefore (e) wasting money. You can replace all that with this:
                                    Source asdf = new StreamSource(in);
                                    1 2 3 Previous Next