5 Replies Latest reply: May 3, 2007 1:44 PM by DrClap RSS

    Classes for checking well formed XML contained in a String...?

    807606
      I have built a desktop application that can use these calls to check if the xml contained in the document called "file" is well formed.

      DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
           DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
           domBuilder.parse(file);

      They are part of the javax.xml.parsers.*; package.

      Problem is that to serve this I need to put it in an applet but since I have to write the xml string to a file local to the server the applet is not working.

      My question is if anyone knows of some classes in the API that will allow me to check if a String of XML is well formed or not.

      I basically need to get a string from a field in a data base in XML format and be able to test it without first writing it to a document so I can get the applet to work.

      If anyone knows something on this or has an alternative suggestion please do tell.

      Thanks.
        • 1. Re: Classes for checking well formed XML contained in a String...?
          DrClap
          That's an FAQ in the XML forum.
          parse(new InputSource(new StringReader(someString)))
          • 2. Re: Classes for checking well formed XML contained in a String...?
            800387
            I have built a desktop application that can use these
            calls to check if the xml contained in the document
            called "file" is well formed.
            Ok. So, the application works fine then?
            DocumentBuilderFactory domFactory =
            DocumentBuilderFactory.newInstance();
            DocumentBuilder domBuilder =
            lder domBuilder = domFactory.newDocumentBuilder();
                 domBuilder.parse(file);

            They are part of the javax.xml.parsers.*; package.

            Problem is that to serve this I need to put it in an
            applet but since I have to write the xml string to a
            file local to the server the applet is not working.
            Huh? You lost me there. Are you getting security exceptions from the applet? If yes, you need to sign the applet. Otherwise, what is not 'working' in the applet compared to the application? Also, why do you need to first write it to a file?
            My question is if anyone knows of some classes in the
            API that will allow me to check if a String of XML is
            well formed or not.
            If the document is not well-formed, the parse() call will throw an exception. Catch this and process as needed.
            I basically need to get a string from a field in a
            data base in XML format and be able to test it
            without first writing it to a document so I can get
            the applet to work.
            If you want to access an external database from an applet, you will need to sign the applet. You should not need to first write it to a file before parsing. Rather, simply use a stream (either InputStream or Reader) and wrap it in a StreamSource object.
            If anyone knows something on this or has an
            alternative suggestion please do tell.

            Thanks.
            - Saish
            • 3. Re: Classes for checking well formed XML contained in a String...?
              807606
              Thanks to all. I missed the XML forum. I'll root around there. My appologies. I know how annoying this can be.

              I think what I'm after is a way to eliminate having to write the string to a file which looks possible. Thanks.
              • 4. Re: Classes for checking well formed XML contained in a String...?
                800387
                Yes. But re-read my reply above. I think you will run into other issues. Also, take a tutorial on Java I/O streams.

                - Saish
                • 5. Re: Classes for checking well formed XML contained in a String...?
                  DrClap
                  Thanks to all. I missed the XML forum. I'll root
                  around there. My appologies. I know how annoying
                  this can be.

                  I think what I'm after is a way to eliminate having
                  to write the string to a file which looks possible.
                  Thanks.
                  That's what the code I posted does. It parses directly from a string.