XMLDocument2 - スコープ指定、グローバル
XMLDocument2 は、XML 文字列から XML データを解析および抽出するための JavaScript オブジェクトラッパーです。
この JavaScript クラスを使用して、XML 文字列からオブジェクト (通常は Web サービスの発動からの戻り値)、または ECC キューの XML ペイロードを作成します。JavaScript ビジネスルールで XMLDocument2 オブジェクトを使用すると、XML 要素と属性から直接、値のクエリーを実行できます。
XML 文字列はツリー構造であり、その構造の一部をノードといいます。XMLDocument2 オブジェクトは、要素とドキュメント要素の 2 つのノードタイプを処理します。要素ノードは、名前と、場合によっては属性と子ノードを持つノードです。ドキュメント要素ノードは、XML ツリーのルートノードです。親ノードを持たない唯一のノードです。
スコープ指定の XMLDocument2 - createElement(文字列 name)
要素ノードを作成して現在のノードに追加します。要素名は、パラメーターとして渡される文字列です。新しい要素にはテキストの子ノードがありません。
| 名前 | タイプ | 説明 |
|---|---|---|
| name | 文字列 | 新しい要素の名前。 |
| タイプ | 説明 |
|---|---|
| XMLNode | 現在の 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);
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 - createElementWithTextValue(文字列 name, 文字列 value)
テキストの子ノードを持つ要素ノードを作成して現在のノードに追加します。
| 名前 | タイプ | 説明 |
|---|---|---|
| name | 文字列 | 追加する要素の名前。 |
| value | 文字列 | 要素のテキスト値。 |
| タイプ | 説明 |
|---|---|
| XMLNode | 現在の 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);
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 - getDocumentElement()
XMLdocument2 オブジェクトのドキュメント要素ノードを取得します。ドキュメント要素ノードはルートノードです。
| 名前 | タイプ | 説明 |
|---|---|---|
| なし |
| タイプ | 説明 |
|---|---|
| XMLNode | ドキュメント要素。 |
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 1234スコープ指定の XMLDocument2 - getFirstNode(文字列 xPath)
指定された xPath の最初のノードを取得します。
<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]" これを回避するには、"/store/resources/resource[@type='bookstore']/book" などの述語なしで xPath を使用し、getFirstNode() および getNextNode() メソッドを使用してスクリプト内のノードをフィルタリングします。
| 名前 | タイプ | 説明 |
|---|---|---|
| xPath | 文字列 | ノードを取得する xPath。 |
| タイプ | 説明 |
|---|---|
| XMLNode | 最初のノード。 |
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());
abcd1234スコープ指定の XMLDocument2 - getNextNode(オブジェクト current)
指定されたノードの次のノードを取得します。
| 名前 | タイプ | 説明 |
|---|---|---|
| current | オブジェクト | 現在のノード。 |
| タイプ | 説明 |
|---|---|
| XMLNode | 次のノード。 |
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
anotherスコープ指定の XMLDocument2 - getNode(文字列 xPath)
xPath で指定されたノードを取得します。
| 名前 | タイプ | 説明 |
|---|---|---|
| xPath | 文字列 | 取得するノードの xPath。 |
| タイプ | 説明 |
|---|---|
| XMLNode | 現在の XML ノード。 |
/*
* 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.NullPointerExceptionスコープ指定の XMLDocument2 - getNodeText(文字列 xPath)
指定された XPath で参照されるノードからテキストの子ノードをすべて取得します。
| 名前 | タイプ | 説明 |
|---|---|---|
| xPath | 文字列 | 取得するテキストの xPath。 |
| タイプ | 説明 |
|---|---|
| 文字列 | 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"));
出力:
abcd1234
スコープ指定の XMLDocument2 - parseXML(文字列 xmlDoc)
XML 文字列を解析し、XMLDocument2 オブジェクトにロードします。
| 名前 | タイプ | 説明 |
|---|---|---|
| xmlDoc | 文字列 | 解析するドキュメント。 |
| タイプ | 説明 |
|---|---|
| ブーリアン | 内容が解析されたかどうかを示すフラグ。 |
この例では、xmlString を解析し、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 - setCurrentElement(XMLNode element)
パラメーターとして渡されたノードを現在のノードにします。
| 名前 | タイプ | 説明 |
|---|---|---|
| 要素 | XMLNode | 現在のノードとして設定する要素ノード。 |
| タイプ | 説明 |
|---|---|
| なし |
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 - setEnableCDATAReporting(ブーリアン enable)
解析後にノードを CDATA として扱うか、通常のテキストとして扱うかを設定します。CDATA レポートはデフォルトで非アクティブになっています。
このメソッドは、 で呼び出す スコープ指定の XMLDocument2 - parseXML(文字列 xmlDoc)必要があります。
関連項目:スコープ指定の XMLNode - isCDATANode()。
| 名前 | タイプ | 説明 |
|---|---|---|
| enable | ブール | CDATA ノードを CDATA として扱うか、通常のテキストノードとして扱うかを示すフラグ。 有効な値:
デフォルト:False |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、CDATA レポートが有効になっている要素で CDATA がどのように解析されるかを示しています。content.getFirstChild() の結果は、テキスト値 Another を保持する要素です。この要素には、CDATA 型として識別する情報が内部的に含まれています。content.getLastChild() の結果は、テキスト値要素を保持する要素です。この要素には、内部的にテキスト型として情報が含まれています。
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"
出力:
another
element
anotherelement
anotherelementスコープ指定の XMLDocument2 - setNamespaceAware(ブーリアン aware)
true に設定すると、XMLDocument2 オブジェクトは XML 名前空間を使用してドキュメントを処理します。
これを設定しないと、名前空間を持つ XML ドキュメントが正しく列挙されず、XPath の検索に失敗します。
| 名前 | タイプ | 説明 |
|---|---|---|
| aware | ブーリアン | true の場合、XMLDocument2 オブジェクトは XML 名前空間を使用してドキュメントを処理します。 |
| タイプ | 説明 |
|---|---|
| なし |
スコープ指定の XMLDocument2 - toString()
XML を含む文字列を返します。
| 名前 | タイプ | 説明 |
|---|---|---|
| なし |
| タイプ | 説明 |
|---|---|
| 文字列 | 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>スコープ指定の XMLDocument2 - XMLDocument2()
XMLDocument2 オブジェクトを作成します。
| 名前 | タイプ | 説明 |
|---|---|---|
| なし |
スコープ指定の XMLDocument2 - XMLDocument2(GlideScriptableInputStream inputStream)
添付ファイルのストリームから XMLDocument2 オブジェクトを作成します。
| 名前 | タイプ | 説明 |
|---|---|---|
| inputStream | GlideScriptableInputStream | XMLDocument2 オブジェクトがカプセル化する入力ストリーム。 |