Problems with XML parsing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Problems with XML parsing

    I am trying to parse a XML file with JAXP.
    Now what's happening to me ..that ..... the XML file is having some DTD which specifies a site DTD definition ....
    <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.s ourceforge.net/dtds/jasperreport.dt d">
    So right now Connection refused error is coming.
    I know why it's coming .... But I want to put off the DTD parsing how could i do that.
    One more thing if a DTD is of site then having no net connection validation can't be done ...????
    What should be validation code for that .....

    Debasis Jana.
  • talonx
    New Member
    • Mar 2008
    • 18

    #2
    Originally posted by dmjpro
    I am trying to parse a XML file with JAXP.
    Now what's happening to me ..that ..... the XML file is having some DTD which specifies a site DTD definition ....
    <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.s ourceforge.net/dtds/jasperreport.dt d">
    So right now Connection refused error is coming.
    I know why it's coming .... But I want to put off the DTD parsing how could i do that.
    One more thing if a DTD is of site then having no net connection validation can't be done ...????
    What should be validation code for that .....

    Debasis Jana.
    Turn off validation when you create the DocumentBuilder Factory.
    Code:
    factory.setValidating(false)
    As for your second question, the answer is too complicated to type here :). It involves creating your own Resolver class to serve the dtd from your machine instead of the parser having to connect to the internet.

    - talonx

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by talonx
      Turn off validation when you create the DocumentBuilder Factory.
      Code:
      factory.setValidating(false)
      As for your second question, the answer is too complicated to type here :). It involves creating your own Resolver class to serve the dtd from your machine instead of the parser having to connect to the internet.

      - talonx

      Thanks for your reply!
      I am now having this code ...
      [code=java]
      DocumentBuilder Factory factory = DocumentBuilder Factory.newInst ance();
      factory.setVali dating(false);
      DocumentBuilder jrxml = factory.newDocu mentBuilder();
      FileInputStream is = new FileInputStream (file);
      Document jrxml_doc = jrxml.parse(is) ;
      [/code]

      Still the error is coming.
      What should I do?

      Debasis Jana.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by dmjpro
        Thanks for your reply!
        I am now having this code ...
        [code=java]
        DocumentBuilder Factory factory = DocumentBuilder Factory.newInst ance();
        factory.setVali dating(false);
        DocumentBuilder jrxml = factory.newDocu mentBuilder();
        FileInputStream is = new FileInputStream (file);
        Document jrxml_doc = jrxml.parse(is) ;
        [/code]

        Still the error is coming.
        What should I do?

        Debasis Jana.
        Which error is coming?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by dmjpro
          Thanks for your reply!
          I am now having this code ...
          [code=java]
          DocumentBuilder Factory factory = DocumentBuilder Factory.newInst ance();
          factory.setVali dating(false);
          DocumentBuilder jrxml = factory.newDocu mentBuilder();
          FileInputStream is = new FileInputStream (file);
          Document jrxml_doc = jrxml.parse(is) ;
          [/code]

          Still the error is coming.
          What should I do?

          Debasis Jana.
          If you had read the API documentation you'd have known that by default that factory
          produces non-validating parsers so the error must be somewhere else. Please
          read before you start ask questions here and hope that others will do your work
          for you. This entire thread has been useless until now because of that non-validating
          default behaviour and you simply didn't know that because you haven't read the
          API documentation.

          kind regards,

          Jos

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by JosAH
            If you had read the API documentation you'd have known that by default that factory
            produces non-validating parsers so the error must be somewhere else. Please
            read before you start ask questions here and hope that others will do your work
            for you. This entire thread has been useless until now because of that non-validating
            default behaviour and you simply didn't know that because you haven't read the
            API documentation.

            kind regards,

            Jos
            Yeah it's OK ......
            Connection Refused Exception coming.
            It tries to connect that DTD resource .....
            How could I solve that?

            Debasis Jana

            Comment

            • talonx
              New Member
              • Mar 2008
              • 18

              #7
              Originally posted by dmjpro
              Yeah it's OK ......
              Connection Refused Exception coming.
              It tries to connect that DTD resource .....
              How could I solve that?

              Debasis Jana
              Try something like

              Code:
              documentBuilder.setEntityResolver(resolver);
              where resolver is your own custom Resolver class implementing the org.xml.sax.Ent ityResolver interface. The implementation should read from your local file on disk and return the dtd (or schema). Am sure you can figure out how.

              - talonx

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Originally posted by talonx
                Try something like

                Code:
                documentBuilder.setEntityResolver(resolver);
                where resolver is your own custom Resolver class implementing the org.xml.sax.Ent ityResolver interface. The implementation should read from your local file on disk and return the dtd (or schema). Am sure you can figure out how.

                - talonx
                I need not to check whether it matches with DTD or not .....
                I just want to bypass the DTD parsing .
                I managed to do that ...by doing this ..
                [code=java]
                jrxml.setEntity Resolver(new EntityResolver( ) {
                public InputSource resolveEntity(S tring publicId, String systemId) throws SAXException,IO Exception {
                //return new InputSource("ht tp://jasperreports.s ourceforge.net/dtds/jasperreport.dt d");
                return new InputSource(new ByteArrayInputS tream(new byte[0]));
                }
                });
                [/code]

                Thanks Jos and Thanks Others.
                Debasis Jana

                Comment

                Working...