XMLUtilJS - グローバル

  • リリースバージョン: Yokohama
  • 更新日 2025年01月30日
  • 所要時間:9分
  • XMLUtilJS スクリプトインクルードは、ディスカバリースクリプトで使用する JavaScript 用の XML ユーティリティメソッドを提供します。

    このスクリプトインクルードは、 ディスカバリー プラグインで提供されます。このスクリプトインクルードは、XML ユーティリティが必要なサーバーサイド ディスカバリー スクリプトで使用します。

    ここで説明するメソッドには、静的変数 XMLUtilJS を使用してアクセスします。

    XMLUtilJS - escapeForXMLText(文字列 text)

    指定された文字列のエスケープテキストを提供します。

    表 : 1. パラメーター
    名前 タイプ 説明
    text 文字列 書式設定するテキスト。
    表 : 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(文字列 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(文字列 text)

    指定された文字列のエスケープされていない XML を提供します。

    表 : 5. パラメーター
    名前 タイプ 説明
    text 文字列 クリーンアップする 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(文字列 XMLvalue)

    XML 値を文字列に変換します。

    表 : 7. パラメーター
    名前 タイプ 説明
    XMLvalue 文字列 変換する 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>