---
sourceDocument: Référence de l’API Xanadu
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/fr-FR/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - fr-FR

ft:publication_title :

    - Référence de l’API Xanadu

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# XMLUtilJS - Global

# XMLUtilJS - Global {#ariaid-title1}

* Rversion finale: Xanadu
* 
* Mis à jour 1 août 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 3 minutes de lecture

L'include de script XMLUtilJS fournit des méthodes utilitaires XML pour JavaScript à utiliser avec Découverte les scripts.

Cet include de script est fourni avec le module d'extension Découverte . Utilisez cet include de script dans n'importe quel script côté Découverte serveur dans lequel vous avez besoin d'utilitaires XML.

Accédez à ces méthodes à l'aide de la variable statique XMLUtilJS.

## XMLUtilJS : escapeForXMLText(Texte de chaîne) {#ariaid-title2}

Fournit un texte d'échappement pour une chaîne donnée.
{#r_XUJ-escapeForXMLText_S__table_upj_wfs_pt__entry__3}

| Nom | Type | Description |
|-|-|-|
| Texte | Chaîne | Texte à mettre en forme. |
[Tableau 1. Paramètres]

{#r_XUJ-escapeForXMLText_S__table_upj_wfs_pt} {#r_XUJ-escapeForXMLText_S__table_vpj_wfs_pt__entry__2}

| Type | Description |
|-|-|
| Chaîne | Le texte mis en forme. |
[Tableau 2. Renvoie]

{#r_XUJ-escapeForXMLText_S__table_vpj_wfs_pt}  
L'exemple suivant montre comment répertorier les données XML échappées dans un document. Voir aussi 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);

Sortie :

    <?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) {#ariaid-title3}

Convertit une chaîne en valeur XML.
{#r_XUJ-stringToValue_S__table_etz_b3s_pt__entry__3}

| Nom | Type | Description |
|-|-|-|
| str | Chaîne | Chaîne à convertir. |
[Tableau 3. Paramètres]

{#r_XUJ-stringToValue_S__table_etz_b3s_pt} {#r_XUJ-stringToValue_S__table_ftz_b3s_pt__entry__2}

| Type | Description |
|-|-|
| Chaîne | Chaîne spécifiée convertie en XML. |
[Tableau 4. Renvoie]

{#r_XUJ-stringToValue_S__table_ftz_b3s_pt}  
L'exemple suivant montre comment transformer une chaîne de XML échappés en sortie XML balisée. Voir aussi 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);

Sortie :

    <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(Texte de chaîne) {#ariaid-title4}

Fournit un XML non échappé pour une chaîne donnée.
{#r_XUJ-unescapeForXMLText_S__table_in5_pgs_pt__entry__3}

| Nom | Type | Description |
|-|-|-|
| Texte | Chaîne | Texte XML à nettoyer. |
[Tableau 5. Paramètres]

{#r_XUJ-unescapeForXMLText_S__table_in5_pgs_pt} {#r_XUJ-unescapeForXMLText_S__table_jn5_pgs_pt__entry__2}

| Type | Description |
|-|-|
| Chaîne | Chaîne XML nettoyée. |
[Tableau 6. Renvoie]

{#r_XUJ-unescapeForXMLText_S__table_jn5_pgs_pt}  
L'exemple suivant montre comment convertir une chaîne XML échappée en sortie XML balisée. Voir aussi 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);

Sortie :

    <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(String XMLvalue) {#ariaid-title5}

Convertit une valeur XML en chaîne.
{#r_XUJ-valueToString_S__table_ix2_4hs_pt__entry__3}

| Nom | Type | Description |
|-|-|-|
| valeur XML | Chaîne | XML à convertir |
[Tableau 7. Paramètres]

{#r_XUJ-valueToString_S__table_ix2_4hs_pt} {#r_XUJ-valueToString_S__table_jx2_4hs_pt__entry__2}

| Type | Description |
|-|-|
| Chaîne | Valeur XML convertie en chaîne. |
[Tableau 8. Renvoie]

{#r_XUJ-valueToString_S__table_jx2_4hs_pt}  
L'exemple suivant montre comment convertir un document XML en chaîne.

    //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);

Sortie :

    <?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>


