programing tip

자바 스크립트에서 xml 엔티티를 이스케이프하는 방법은 무엇입니까?

itbloger 2020. 10. 28. 07:48
반응형

자바 스크립트에서 xml 엔티티를 이스케이프하는 방법은 무엇입니까?


JavaScript (서버 측 nodejs)에서 xml을 출력으로 생성하는 프로그램을 작성하고 있습니다.

문자열을 연결하여 xml을 작성하고 있습니다.

str += '<' + key + '>';
str += value;
str += '</' + key + '>';

문제는 다음 value과 같습니다. '&', '>'또는 '<'? 그 캐릭터를 탈출하는 가장 좋은 방법은 무엇입니까?

또는 XML 엔티티를 이스케이프 할 수있는 자바 스크립트 라이브러리가 있습니까?


HTML 인코딩은 단순히 대체 &, ", ', <그리고 >자신의 엔티티 등가물로 문자를. 순서가 중요합니다. &문자를 먼저 바꾸지 않으면 일부 엔티티를 이중 인코딩합니다.

if (!String.prototype.encodeHTML) {
  String.prototype.encodeHTML = function () {
    return this.replace(/&/g, '&amp;')
               .replace(/</g, '&lt;')
               .replace(/>/g, '&gt;')
               .replace(/"/g, '&quot;')
               .replace(/'/g, '&apos;');
  };
}

@Johan BW de Vries가 지적했듯이 이것은 태그 이름에 문제가있을 것이므로 이것이 유일한 용도로 사용된다는 가정을 분명히하고 싶습니다.value

반대로 HTML 엔터티 1 을 디코딩 &amp;하려면 &엔터티를 이중 디코딩하지 않도록 다른 모든 항목 이후에 디코딩해야합니다 .

if (!String.prototype.decodeHTML) {
  String.prototype.decodeHTML = function () {
    return this.replace(/&apos;/g, "'")
               .replace(/&quot;/g, '"')
               .replace(/&gt;/g, '>')
               .replace(/&lt;/g, '<')
               .replace(/&amp;/g, '&');
  };
}

1 개 포함뿐만 아니라 기초, &copy;©또는 다른 것들


도서관에 관한 한. Underscore.js (또는 원하는 경우 Lodash )는 _.escape이 기능을 수행 하는 방법을 제공 합니다.


이것은 동일한 결과로 좀 더 효율적일 수 있습니다.

function escapeXml(unsafe) {
    return unsafe.replace(/[<>&'"]/g, function (c) {
        switch (c) {
            case '<': return '&lt;';
            case '>': return '&gt;';
            case '&': return '&amp;';
            case '\'': return '&apos;';
            case '"': return '&quot;';
        }
    });
}

jQuery가있는 경우 다음은 간단한 솔루션입니다.

  String.prototype.htmlEscape = function() {
    return $('<div/>').text(this.toString()).html();
  };

다음과 같이 사용하십시오.

"<foo&bar>".htmlEscape(); -> "&lt;foo&amp;bar&gt"


아래 방법을 사용할 수 있습니다. 쉽게 액세스 할 수 있도록 프로토 타입에 추가했습니다. 나는 또한 부정적인 미리보기를 사용하여 메서드를 두 번 이상 호출하면 일을 엉망으로 만들지 않습니다.

용법:

 var original = "Hi&there";
 var escaped = original.EncodeXMLEscapeChars();  //Hi&amp;there

디코딩은 XML 파서에서 자동으로 처리됩니다.

방법 :

//String Extenstion to format string for xml content.
//Replces xml escape chracters to their equivalent html notation.
String.prototype.EncodeXMLEscapeChars = function () {
    var OutPut = this;
    if ($.trim(OutPut) != "") {
        OutPut = OutPut.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
        OutPut = OutPut.replace(/&(?!(amp;)|(lt;)|(gt;)|(quot;)|(#39;)|(apos;))/g, "&amp;");
        OutPut = OutPut.replace(/([^\\])((\\\\)*)\\(?![\\/{])/g, "$1\\\\$2");  //replaces odd backslash(\\) with even.
    }
    else {
        OutPut = "";
    }
    return OutPut;
};

나는 원래 프로덕션 코드에서 받아 들여진 대답을 사용했으며 실제로 많이 사용하면 실제로 느리다는 것을 알았습니다. 다음은 훨씬 더 빠른 솔루션입니다 (두 배 이상의 속도로 실행).

   var escapeXml = (function() {
        var doc = document.implementation.createDocument("", "", null)
        var el = doc.createElement("temp");
        el.textContent = "temp";
        el = el.firstChild;
        var ser =  new XMLSerializer();
        return function(text) {
            el.nodeValue = text;
            return ser.serializeToString(el);
        };
    })();

console.log(escapeXml("<>&")); //&lt;&gt;&amp;

시도해 볼 수 있습니다.

function encodeXML(s) {
  const dom = document.createElement('div')
  dom.textContent = s
  return dom.innerHTML
}

참고


기술적으로 &, <및>는 유효한 XML 엔티티 이름 문자가 아닙니다. 키 변수를 신뢰할 수없는 경우 필터링해야합니다.

HTML 엔티티로 이스케이프하려면 http://www.strictly-software.com/htmlencode 와 같은 것을 사용할 수 있습니다 .


if something is escaped from before, you could try this since this will not double escape like many others

function escape(text) {
    return String(text).replace(/(['"<>&'])(\w+;)?/g, (match, char, escaped) => {
        if(escaped) 
            return match

        switch(char) {
            case '\'': return '&quot;'
            case '"': return '&apos;'
            case '<': return '&lt;'
            case '>': return '&gt;'
            case '&': return '&amp;'
        }
    })
}

This is simple:

sText = ("" + sText).split("<").join("&lt;").split(">").join("&gt;").split('"').join("&#34;").split("'").join("&#39;");

참고URL : https://stackoverflow.com/questions/7918868/how-to-escape-xml-entities-in-javascript

반응형