XMLUtilJS - 전역

  • 릴리스 버전: Yokohama
  • 업데이트 날짜 2025년 01월 30일
  • 읽기8분
  • XMLUtilJS 스크립트 포함은 스크립트와 함께 디스커버리 사용할 JavaScript에 대한 XML 유틸리티 메서드를 제공합니다.

    이 스크립트 포함은 플러그인과 디스커버리 함께 제공됩니다. XML 유틸리티가 필요한 서버 측 디스커버리 스크립트에 이 스크립트 포함을 사용하십시오.

    정적 변수 XMLUtilJS를 사용하여 이러한 메서드에 액세스합니다.

    XMLUtilJS - escapeForXMLText(문자열 텍스트)

    지정된 문자열에 대한 이스케이프 텍스트를 제공합니다.

    표 1. 매개변수
    이름 유형 설명
    텍스트 문자열 서식을 지정할 텍스트입니다.
    표 2. 반환
    유형 설명
    문자열 서식이 지정된 텍스트입니다.

    다음 예제에서는 이스케이프된 XML 데이터를 문서에 나열하는 방법을 보여 줍니다. stringToValue () 도 참조하십시오 .

    // Create an XML document to work with
    var doc = new XMLDocument2();
    
    // Add an element called catalog
    var catalog = doc.createElement('catalog');
    
    // Set current element to the one previously made to add more elements
    doc.setCurrentElement(catalog);
    
    // Add an element called catalog
    var bookAttribute = doc.createElement('book');
    
    // Add an attribute to the element created
    bookAttribute.setAttribute('id', 'bk101');
    
    // Create multiple elements with a defined value
    doc.createElementWithTextValue('author', 'Gambardella, Matthew');
    doc.createElementWithTextValue('title', 'XML Developer');
    doc.createElementWithTextValue('genre', 'Computer');
    doc.createElementWithTextValue('price', '44.95');
    
    // Pass the created XML document as a string to the XMLUtilJS API function
    var escapedXML = XMLUtilJS.escapeForXMLText(doc.toString());
    
    // Print the escapedXML. If an invalid XML is created, the original value will be returned
    gs.print(escapedXML);

    출력:

    <?xml version="1.0" encoding="UTF-8"?><catalog><book id="bk101"/><author>Gambardella, Matthew</author><title>XML Developer</title><genre>Computer</genre><price>44.95</price></catalog>

    XMLUtilJS - stringToValue(String str)

    문자열을 XML 값으로 변환합니다.

    표 3. 매개변수
    이름 유형 설명
    STR 문자열 변환할 문자열입니다.
    표 4. 반환
    유형 설명
    문자열 지정된 문자열이 XML로 변환됩니다.

    다음 예제에서는 이스케이프된 XML 문자열을 태그가 지정된 XML 출력으로 변환하는 방법을 보여 줍니다. escapeForXMLText()도 참조하십시오.

    //Example of an escaped XML string, previously generated by XMLUtilJS.escapeForXMLText()
    var escapedXML = '<catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer\'s Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description></book></catalog>';
    
    //Pass in escaped XML text and store output in a variable
    var output = XMLUtilJS.stringToValue(escapedXML);
    
    // If the input value is a string value of NULL, the output will be null, otherwise an unescaped XML string value.
    gs.print(output);

    출력:

    <catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description></book></catalog>

    XMLUtilJS - unescapeForXMLText(문자열 텍스트)

    지정된 문자열에 대해 이스케이프되지 않은 XML을 제공합니다.

    표 5. 매개변수
    이름 유형 설명
    텍스트 문자열 정리할 XML 텍스트입니다.
    표 6. 반환
    유형 설명
    문자열 정리된 XML 문자열입니다.

    다음 예제에서는 이스케이프된 XML 문자열을 태그가 지정된 XML 출력으로 변환하는 방법을 보여 줍니다. escapeForXMLText()도 참조하십시오.

    //Example of an escaped XML string, previously generated by XMLUtilJS.escapeForXMLText()
    var escapedXML = '<catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer\'s Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description></book></catalog>';
    
    //Pass in escaped XML text and store output in a variable
    var output = XMLUtilJS.unescapeForXMLText(escapedXML);
    
    
    //Print the escapedXML. If the XML is invalid, the original value will be returned
    gs.print(output);

    출력:

    <catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description></book></catalog>
    

    XMLUtilJS - valueToString(문자열 XML값)

    XML 값을 문자열로 변환합니다.

    표 7. 매개변수
    이름 유형 설명
    XML값 문자열 변환할 XML
    표 8. 반환
    유형 설명
    문자열 문자열로 변환된 XML 값입니다.

    다음 예제에서는 XML 문서를 문자열로 변환하는 방법을 보여 줍니다.

    //Create an XML document to work with
    var doc = new XMLDocument2();
    
    //Add an element called catalog
    var catalog = doc.createElement("catalog");
    
    //Set our current element to the one previously made to add further elements
    doc.setCurrentElement(catalog);
    
    //Add an element called catalog
    var bookAttribute = doc.createElement("book");
    
    //Add an attribute to the element created
    bookAttribute.setAttribute("id" , "bk101");
    
    //create multiple elements with a defined value
    doc.createElementWithTextValue("author" , "Gambardella, Matthew");
    doc.createElementWithTextValue("title" , "XML Developer");
    doc.createElementWithTextValue("genre" , "Computer");
    doc.createElementWithTextValue("price" , "44.95");
    
    //Pass the created XML document as a string to the XMLUtilJS API function.
    var escapedXML = XMLUtilJS.valueToString(doc.toString());
    
    // Print the results
    // Returns NULL if XML is invalid
    gs.print(escapedXML);

    출력:

    <?xml version="1.0" encoding="UTF-8"?><catalog><book id="bk101"/><author>Gambardella, Matthew</author><title>XML Developer</title><genre>Computer</genre><price>44.95</price></catalog>