Check for XML errors using JavaScript

前端 未结 5 1644
耶瑟儿~
耶瑟儿~ 2020-12-24 13:36

Question: How do I syntax-check my XML in modern browsers (anything but IE)?

I\'ve seen a page on W3Schools which includes an XML syntax-checker. I don\'t kno

5条回答
  •  有刺的猬
    2020-12-24 14:19

    Edit: Here is a more concise example, from MDN:

    var xmlString = 'hey!';
    var domParser = new DOMParser();
    var dom = domParser.parseFromString(xmlString, 'text/xml');
    
    // print the name of the root element or error message
    dump(dom.documentElement.nodeName == 'parsererror' ? 'error while parsing' : dom.documentElement.nodeName);
    

提交回复
热议问题