XMLDocument2 - Délimité, global
L’API XMLDocument2 est une couche d’objet JavaScript permettant d’analyser et d’extraire des données XML à partir d’une chaîne XML.
Utilisez cette classe JavaScript pour créer un objet à partir d’une chaîne XML, généralement une valeur de retour d’un appel de service Web, ou de la charge utile XML de la file d’attente ECC. L’utilisation de l’objet XMLDocument2 dans une règle métier JavaScript vous permet d’interroger directement des valeurs à partir des éléments et attributs XML.
Une chaîne XML a une structure arborescente et les parties de la structure sont appelées nœuds. Un objet XMLDocument2 traite deux types de nœuds, l’élément et l’élément document. Un nœud d’élément est un nœud portant un nom et éventuellement des attributs et des nœuds enfants. Un noeud document-element est le noeud racine de l’arborescence XML. C’est le seul nœud sans nœud parent.
XMLDocument2 inclus dans le périmètre : XMLDocument2()
Crée un objet XMLDocument2.
| Nom | Type | Description |
|---|---|---|
| Aucun |
XMLDocument2 inclus dans le périmètre : XMLDocument2( GlideScriptableInputStream, inputStream)
Crée un objet XMLDocument2 à partir d’un flux de pièces jointes.
| Nom | Type | Description |
|---|---|---|
| inputStream (en anglais seulement) | GlideScriptableInputStream | Flux d’entrée encapsulé par l’objet XMLDocument2. |
XMLDocument2 inclus dans le périmètre : createElement(nom de chaîne)
Crée et ajoute un nœud d’élément au nœud actuel. Le nom de l’élément est la chaîne transmise en tant que paramètre. Le nouvel élément n’a pas de nœuds enfants de texte.
| Nom | Type | Description |
|---|---|---|
| nom | Chaîne | Nom du nouvel élément. |
| Type | Description |
|---|---|
| Nœud XML | Nœud XML actuel. |
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
xmlDoc.createElement("new2");
gs.info(xmlDoc);
?xml version="1.0" encoding="UTF-8"?>
<test>
<one>
<two att="xxx">abcd1234</two>
<three att="yyy" boo="yah">1234abcd</three>
<two>another</two>
</one>
<number>1234</number>
<new2></new2>
</test>XMLDocument2 inclus dans le périmètre : createElementWithTextValue(nom de chaîne, valeur de chaîne)
Crée et ajoute un nœud d’élément avec un nœud enfant de texte au nœud actuel.
| Nom | Type | Description |
|---|---|---|
| nom | Chaîne | Nom de l’élément à ajouter. |
| valide | Chaîne | Valeur de texte de l’élément. |
| Type | Description |
|---|---|
| Nœud XML | Nœud XML actuel. |
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
xmlDoc.createElementWithTextValue("new", "test");
gs.info(xmlDoc);
<?xml version="1.0" encoding="UTF-8"?>
<test>
<one>
<two att="xxx">abcd1234</two>
<three att="yyy" boo="yah">1234abcd</three>
<two>another</two>
</one>
<number>1234</number>
<new>test</new>
</test>XMLDocument2 inclus dans le périmètre : getDocumentElement()
Obtient le nœud d’élément de document de l’objet XMLdocument2. Le nœud d’élément de document est le nœud racine.
| Nom | Type | Description |
|---|---|---|
| Aucun |
| Type | Description |
|---|---|
| Nœud XML | L’élément de document. |
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
//returns the root node of the document tree.
var rootNode = xmlDoc.getDocumentElement();
gs.info(rootNode.getTextContent());
abcd1234 1234abcd another 1234XMLDocument2 inclus dans le périmètre : getFirstNode(String xPath)
Obtient le premier nœud dans le xPath spécifié.
<store>
<resources company="ABC Inc">
<resources type="servers" />
</resources>
<resources company="XYZ Inc">
<resource type="bookstore">
<book>
<title>Windows</title>
<price>10</price>
</book>
<book year="2009">
<title>Harry Potter</title>
<price>50</price>
</book>
<book year="1999">
<title>Learning XML</title>
<price>120</price>
</book>
<book year="2019">
<title>Learning Java</title>
<price>99</price>
</book>
</resource>
</resources>
</store> "/store/resources/resource[@type='bookstore']/book[@year='1999']",
"/store/resources/resource[@type='bookstore']/book[@year]",
"/store/resources/resource[@type='bookstore']/book[price > 100]" "/store/resources/resource[@type='bookstore']/book[2]",
"/store/resources/resource[@type='bookstore']/book[last()]",
"/store/resources/resource[@type='bookstore']/book[position()>2]" Pour contourner ce problème, utilisez xPath sans prédicats, tels que « /store/resources/resource[@type='bookstore']/book »), puis filtrez les nœuds du script à l’aide des méthodes getFirstNode() et getNextNode().
| Nom | Type | Description |
|---|---|---|
| xPath | Chaîne | xPath à partir duquel obtenir le nœud. |
| Type | Description |
|---|---|
| Nœud XML | Le premier nœud. |
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
var foo = xmlDoc.getFirstNode('/test/one/two');
gs.info(foo.getTextContent());
abcd1234XMLDocument2 inclus dans le périmètre : getNextNode(Object current)
Obtient le nœud après le nœud spécifié.
| Nom | Type | Description |
|---|---|---|
| current | Objet | Le nœud actuel. |
| Type | Description |
|---|---|
| Nœud XML | Le nœud suivant. |
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
var foo = xmlDoc. getFirstNode('/test/one/two');
var foo2 = xmlDoc.getNextNode(foo);
gs.info(foo.getTextContent());
gs.info(foo2.getTextContent());
abcd1234
anotherXMLDocument2 inclus dans le périmètre : getNode(String xPath)
Obtient le nœud spécifié dans xPath.
| Nom | Type | Description |
|---|---|---|
| xPath | Chaîne | xChemin du nœud à obtenir. |
| Type | Description |
|---|---|
| Nœud XML | Nœud XML actuel. |
/*
* Checks if given node indeed of given tag.
* Params:
* tag - String, name of tag to check
* node - XMLNode, node in which to check for the tag
* Returns:
* true, if tag is present
* false, otherwise
*/
function isNodeOfTag (node, tag) {
try {
if (tag == node.getNodeName()) {
gs.info("Given node belongs to tag " + tag);
return true;
}
} catch (error) {
gs.error("Given node might not belong to tag " + tag + ". Error while checking: " + error);
return false;
}
}
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
var node = xmlDoc.getNode("/test/one/two"); // tag 'two' is present in this XML.
isNodeOfTag(node, "two");
var node = xmlDoc.getNode("/test/one/four"); // tag 'four' is not present in this XML.
isNodeOfTag(node, "four");
Given node belongs to tag two
Given node might not belong to tag four. Error while checking: java.lang.NullPointerExceptionXMLDocument2 inclus dans le périmètre : getNodeText(String xPath)
Obtient tous les nœuds enfants de texte à partir du nœud référencé dans le XPath spécifié.
| Nom | Type | Description |
|---|---|---|
| xPath | Chaîne | XPath du texte à obtenir. |
| Type | Description |
|---|---|
| Chaîne | Textez les enfants dans le XPath. |
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
gs.info(xmlDoc.getNodeText("//two"));
Sortie :
abcd1234
XMLDocument2 inclus dans le périmètre : parseXML(String xmlDoc)
Analyse la chaîne XML et la charge dans l’objet XMLDocument2.
| Nom | Type | Description |
|---|---|---|
| xmlDoc (en anglais seulement) | Chaîne | Le document à analyser. |
| Type | Description |
|---|---|
| Booléen | Marqueur indiquant si le contenu a été analysé. |
Cet exemple analyse la xmlString et la charge dans l’objet xmlDocument2.
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
var rootNode = xmlDoc.getDocumentElement();
XMLDocument2 inclus dans le périmètre : setCurrentElement(élément XMLNode)
Fait du nœud transmis en tant que paramètre le nœud actuel.
| Nom | Type | Description |
|---|---|---|
| élément | Nœud XML | Le nœud d’élément à définir en tant que nœud actuel. |
| Type | Description |
|---|---|
| nul |
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
//returns the root node of the document tree.
var rootNode = xmlDoc.getDocumentElement(); //returns org.w3c.dom.Element
// sets the root node as the current element
xmlDoc.setCurrentElement(rootNode);
XMLDocument2 inclus dans le champ d’application : setEnableCDATAReporting(Boolean enable)
Définit si les nœuds sont traités comme CDATA ou comme du texte normal après l’analyse. Le reporting CDATA est désactivé par défaut.
Cette méthode doit être appelée avec XMLDocument2 inclus dans le périmètre : parseXML(String xmlDoc).
Voir aussi : XMLNode inclus dans le périmètre : isCDATANode().
| Nom | Type | Description |
|---|---|---|
| activer | Booléen | Marqueur indiquant s’il faut traiter les nœuds CDATA en tant que nœuds CDATA ou nœuds de texte standard. Valeurs valides :
Valeur par défaut : false |
| Type | Description |
|---|---|
| Aucun |
L’exemple suivant montre comment CDATA est analysé dans un élément avec la génération de rapports CDATA activée. Le résultat de content.getFirstChild() est un élément qui contient une autre valeur de texte. Cet élément contient en interne les informations qui l’identifient en tant que type CDATA. Le résultat de content.getLastChild() est un élément qui contient l’élément de valeur textuelle. Cet élément contient les informations en interne sous forme de type texte.
var xmlString = "<test>" +
" <one>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two><![CDATA[another]]>element</two>" +
" </one>" +
" <number>1234</number>"
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.setEnableCDATAReporting(true); // Enable CDATA reporting
xmlDoc.parseXML(xmlString);
var content = xmlDoc.getFirstNode('/test/one/two');
gs.info(content.getFirstChild().getTextContent()); // prints "another"
gs.info(content.getLastChild().getTextContent()); // prints "element"
Sortie :
another
element
anotherelement
anotherelementXMLDocument2 inclus dans le périmètre : setNamespaceAware(compatible avec les booléens)
Lorsqu’il est défini sur vrai, l’objet XMLDocument2 traite le document avec des espaces de noms XML.
Si vous ne définissez pas cette option, un document XML avec des espaces de noms ne sera pas énuméré correctement et une recherche XPath échouera.
| Nom | Type | Description |
|---|---|---|
| conscient | Booléen | Si vrai, l’objet XMLDocument2 traite le document avec des espaces de noms XML. |
| Type | Description |
|---|---|
| nul |
XMLDocument2 inclus dans le périmètre : toString()
Renvoie une chaîne contenant le code XML.
| Nom | Type | Description |
|---|---|---|
| Aucun |
| Type | Description |
|---|---|
| Chaîne | Chaîne contenant le code XML. |
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
gs.info(xmlDoc.toString());
<?xml version="1.0" encoding="UTF-8"?>
<test>
<one>
<two att="xxx">abcd1234</two>
<three att="yyy" boo="yah">1234abcd</three>
<two>another</two>
</one>
<number>1234</number>
</test>