Code XML : dans le champ d’application, global
L’API XMLNode fournit des méthodes pour interroger les valeurs à partir de nœuds XML. Les XMLNodes sont extraits d’objets XMLDocument2, qui contiennent des chaînes XML.
Il n’existe aucun constructeur permettant de créer une instance autonome d’un objet XMLNode. Utilisez plutôt la méthode createElement() de XMLDocument2, qui ajoute un nœud à un document existant.
Code XML dans le champ d’application : getAttribute (attribut de chaîne)
Obtient la valeur de l’attribut.
| Nom | Type | Description |
|---|---|---|
| attribut | Chaîne | Nom de l’attribut. |
| Type | Description |
|---|---|
| Chaîne | Valeur de l’attribut. |
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('//two');
gs.info(node.getAttribute('att'));
Sortie :
xxx
XMLNode dans le champ d’application : getAttributes()
Renvoie un objet contenant les attributs du nœud en tant que propriétés avec valeurs.
| Nom | Type | Description |
|---|---|---|
| Aucun |
| Type | Description |
|---|---|
| Objet | Contient des paires nom-valeur où le nom est l’attribut et la valeur est la valeur de l’attribut. |
XMLNode dans le champ d’application : getChildNodeIterator()
Obtient un objet XMLNodeIterator qui peut être utilisé pour parcourir la liste des nœuds enfants.
| Nom | Type | Description |
|---|---|---|
| Aucun |
| Type | Description |
|---|---|
| XMLNodeIterator | Objet itérateur de 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 node = xmlDoc.getNode('//one');
var iter= node.getChildNodeIterator();
gs.info(iter.hasNext());
Code XML dans le champ d’application : getFirstChild()
Obtient le premier nœud enfant du nœud.
| Nom | Type | Description |
|---|---|---|
| Aucun |
| Type | Description |
|---|---|
| Code XML | Premier nœud enfant du 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 node = xmlDoc.getNode('//one');
gs.info(node.getFirstChild());
<two att="xxx">abcd1234</two>XMLNode dans le champ d’application : getLastChild()
Obtient le dernier nœud enfant du nœud.
| Nom | Type | Description |
|---|---|---|
| Aucun |
| Type | Description |
|---|---|
| Code XML | Le dernier enfant du 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 node = xmlDoc.getNode('//one');
gs.info(node.getLastChild());
<two>another</two>Code XML dans le champ d’application : getNodeName()
Obtient le nom du nœud. Le nom d’un nœud est déterminé par le type de nœud. Le nom d’un nœud document-élément est #document. Le nom d’un nœud de texte est #text. Le nom d’un nœud d’élément est le nom de l’élément.
| Nom | Type | Description |
|---|---|---|
| Aucun |
| Type | Description |
|---|---|
| Chaîne | Nom du 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 node = xmlDoc.getNode('//two');
gs.info(node.getNodeName());
Sortie :
two
XMLNode dans le champ d’application : getNodeValue()
Obtient la valeur du nœud. La valeur d’un nœud est déterminée par le type de nœud. Les nœuds élément et document-élément renvoient null.
| Nom | Type | Description |
|---|---|---|
| Aucun |
| Type | Description |
|---|---|
| Chaîne | Valeur du 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 node = xmlDoc.getNode('//two');
gs.info(node.getNodeValue());
Sortie :
null
XMLNode dans le champ d’application : getTextContent()
Obtient le contenu textuel du nœud actuel. Le contenu textuel d’un nœud se compose de tous les nœuds de texte enfants du nœud
| Nom | Type | Description |
|---|---|---|
| Aucun |
| Type | Description |
|---|---|
| Chaîne | Contenu textuel du nœud 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);
var node = xmldoc.getNode('//one/two');
gs.info(node.getTextContent());
Sortie :
abcd1234
Code XML dans le champ d’application : hasAttribute(Attribut de chaîne)
Détermine si le nœud possède l’attribut spécifié.
| Nom | Type | Description |
|---|---|---|
| attribut | Chaîne | Nom de l’attribut à vérifier. |
| Type | Description |
|---|---|
| Booléen | Vrai si le nœud possède l’attribut. |
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('//two');
gs.info(node.hasAttribute('att'));
Sortie :
true
XMLNode dans le champ d’application : isCDATANode()
Indique si le nœud CDATA est conservé en tant que nœud séparé.
Utilisez la méthode pour vous assurer que les XMLDocument2 dans le champ d’application : setEnableCDATAReporting(Boolean enable) nœuds CDATA sont préservés et ne sont pas traités comme du texte.
| Nom | Type | Description |
|---|---|---|
| Aucun |
| Type | Description |
|---|---|
| Booléen | Marqueur indiquant si un nœud interrogé est de type CDATA ou du texte brut. Valeurs valides :
|
L’exemple suivant montre comment analyser une chaîne XML avec la génération de rapports CDATA activée à l’aide XMLDocument2 dans le champ d’application : setEnableCDATAReporting(Boolean enable) de . Le code utilise isCDATANode() pour montrer que le premier nœud interrogé dans la chaîne XML est un nœud CDATA.
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>"+
" <four><![CDATA[another]]>element</four>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.setEnableCDATAReporting(true); // Enables CDATA reporting
xmlDoc.parseXML(xmlString);
var content = xmlDoc.getFirstNode('/test/one/four');
gs.info(content.getFirstChild().isCDATANode());
Sortie :
true
XMLNode dans le champ d’application : toString()
Renvoie la valeur de chaîne du nœud actuel.
| Nom | Type | Description |
|---|---|---|
| Aucun |
| Type | Description |
|---|---|
| Chaîne | Valeur de chaîne du nœud 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);
var node = xmlDoc.getNode('//one');
gs.info(node.toString());
<one>
<two att="xxx">abcd1234</two>
<three att="yyy" boo="yah">1234abcd</three>
<two>another</two>
</one>