Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test to see if a string is an XML document or not in jQuery

I have a string that may or may not contain valid xml content. How can I test to see if that particular string is a valid xml document or not? I would prefer this to be in jQuery or just plain javascript.

thanks.

like image 218
Niner Avatar asked Dec 29 '25 10:12

Niner


1 Answers

You can try this JQuery code:

function isXML(xml){
    try {
        xmlDoc = $.parseXML(xml); //is valid XML
        return true;
    } catch (err) {
        // was not XML
        return false;
    }
}
like image 172
adearriba Avatar answered Dec 30 '25 23:12

adearriba