XML Parser/validator in Java script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • siva538
    New Member
    • Jun 2007
    • 44

    XML Parser/validator in Java script

    Hi All,

    I want some good implementation in Javascript for validating XML data.

    If user enters some text in the textarea control, before XML data is saved in the database, the validation should happen in terms of whether it is well-formed or not, etc. Can you please point me to the same?

    Thanks in advance!

    Regds,
    Sivakumar
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use a server-side language to do the validation. You could make an Ajax request to validate the XML string.

    Comment

    • siva538
      New Member
      • Jun 2007
      • 44

      #3
      Originally posted by acoder
      Use a server-side language to do the validation. You could make an Ajax request to validate the XML string.
      Hi acoder,

      This is my last resort. I want to implement using client side validation.

      Please help !

      Thanks,
      Sivakumar

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        See http://www.w3schools.com/Xml/xml_validator.asp

        Comment

        • rnd me
          Recognized Expert Contributor
          • Jun 2007
          • 427

          #5
          if you run it through a parser, the parser will error out if the xml is semantically broken.

          true validity is harder to validate, and i am not aware of any javascript-based validator, not that it's impossible.


          this might interest you for the well-formedness part:
          Code:
          function makeWellFormed(str){
              var b = document.createElement("b");
              b.innerHTML = str;
            return b.innerHTML;
          }
          
          makeWellFormed("<ber>frefg</ber") //returns "<ber>frefg</ber>"
          makeWellFormed("<ber>frefg</ber>") //returns "<ber>frefg</ber>"
          makeWellFormed("<ber>frefg") //returns "<ber>frefg</ber>"

          Comment

          • siva538
            New Member
            • Jun 2007
            • 44

            #6
            Originally posted by rnd me
            if you run it through a parser, the parser will error out if the xml is semantically broken.

            true validity is harder to validate, and i am not aware of any javascript-based validator, not that it's impossible.


            this might interest you for the well-formedness part:
            Code:
            function makeWellFormed(str){
                var b = document.createElement("b");
                b.innerHTML = str;
              return b.innerHTML;
            }
            
            makeWellFormed("<ber>frefg</ber") //returns "<ber>frefg</ber>"
            makeWellFormed("<ber>frefg</ber>") //returns "<ber>frefg</ber>"
            makeWellFormed("<ber>frefg") //returns "<ber>frefg</ber>"
            Thanks for the reply! the function you provided just returns the wellformed strings. I was looking for some xml validation code.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by siva538
              Thanks for the reply! the function you provided just returns the wellformed strings. I was looking for some xml validation code.
              Have you seen the link I posted?

              Comment

              • siva538
                New Member
                • Jun 2007
                • 44

                #8
                Originally posted by acoder
                Have you seen the link I posted?
                Yes! I have seen it. But the problem with that is, it is a validation of xml using a DTD schema. I was looking for a gereric validator for just well formedness.

                In the mean time we had written a validator ourselves. But unfortunately that was not very robust :(

                Please help if you can !

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by siva538
                  Yes! I have seen it. But the problem with that is, it is a validation of xml using a DTD schema. I was looking for a gereric validator for just well formedness.
                  The first section (Syntax check your XML) does exactly that!

                  Comment

                  • siva538
                    New Member
                    • Jun 2007
                    • 44

                    #10
                    Originally posted by acoder
                    The first section (Syntax check your XML) does exactly that!
                    This is right on spot and thanks for pointing me to this. Earlier when you posted this I didn't look at it correctly. But now it really solved the problem.

                    Thank you very very much for helping me on this !!

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      No problem, you're welcome. Glad I was able to help.

                      Comment

                      Working...