Check for XML errors using JavaScript

前端 未结 5 1676
耶瑟儿~
耶瑟儿~ 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:14

    Just F12 to enter developer mode and check the source there you can then search validateXML and you are to locate a very long complete XML checker for your reference.

    I am using react and stuff using the DOMParser to present the error message as:

      handleXmlCheck = () => {
        const { fileContent } = this.state;
        const parser = new window.DOMParser();
        const theDom = parser.parseFromString(fileContent, 'application/xml');
        if (theDom.getElementsByTagName('parsererror').length > 0) {
          showErrorMessage(theDom.getElementsByTagName('parsererror')[0].getElementsByTagName('div')[0].innerHTML);
        } else {
          showSuccessMessage('Valid Xml');
        }
      }
    

提交回复
热议问题