XMLDocument2 - 범위가 지정됨, 전역
XMLDocument2 API는 XML 문자열에서 XML 데이터를 구문 분석하고 추출하기 위한 JavaScript 오브젝트 래퍼입니다.
이 JavaScript 클래스를 사용하여 XML 문자열(일반적으로 웹 서비스 호출의 반환 값 또는 ECC 큐의 XML 페이로드)에서 개체를 만듭니다. JavaScript 비즈니스 규칙에서 XMLDocument2 객체를 사용하면 XML 요소 및 특성의 값을 직접 쿼리할 수 있습니다.
XML 문자열에는 트리 구조가 있으며 구조체의 일부를 노드라고 합니다. XMLDocument2 개체는 element와 document element의 두 가지 노드 유형을 처리합니다. 요소 노드는 이름과 속성 및 하위 노드가 있는 노드입니다. 문서 요소 노드는 XML 트리의 루트 노드입니다. 상위 노드가 없는 유일한 노드입니다.
범위가 지정된 XMLDocument2 - XMLDocument2()
XMLDocument2 객체를 만듭니다.
| 이름 | 유형 | 설명 |
|---|---|---|
| 없음 |
범위가 지정된 XMLDocument2 - XMLDocument2(GlideScriptableInputStream inputStream)
첨부 파일 스트림에서 XMLDocument2 객체를 작성합니다.
| 이름 | 유형 | 설명 |
|---|---|---|
| inputStream | GlideScriptableInputStream | XMLDocument2 객체가 캡슐화하는 입력 스트림입니다. |
범위가 지정된 XMLDocument2 - createElement(문자열 이름)
요소 노드를 작성하여 현재 노드에 추가합니다. 요소 이름은 매개 변수로 전달된 문자열입니다. 새 요소에는 텍스트 자식 노드가 없습니다.
| 이름 | 유형 | 설명 |
|---|---|---|
| 이름 | 문자열 | 새 요소의 이름입니다. |
| 유형 | 설명 |
|---|---|
| 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(문자열 이름, 문자열 값)
텍스트 하위 노드가 있는 요소 노드를 작성하여 현재 노드에 추가합니다.
| 이름 | 유형 | 설명 |
|---|---|---|
| 이름 | 문자열 | 추가할 요소의 이름입니다. |
| 값 | 문자열 | 요소의 텍스트 값입니다. |
| 유형 | 설명 |
|---|---|
| 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]" 이 문제를 해결하려면 조건자 없이 xPath( 예: "/store/resources/resource[@type='bookstore']/book") 를 사용한 다음 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 | 객체 | 현재 노드입니다. |
| 유형 | 설명 |
|---|---|
| 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(String 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 요소)
현재 노드에 매개변수로 전달된 노드를 만듭니다.
| 이름 | 유형 | 설명 |
|---|---|---|
| 요소 | XMLNode | 현재 노드로 설정할 요소 노드입니다. |
| 유형 | 설명 |
|---|---|
| void |
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(부울 사용)
구문 분석 후 노드를 CDATA 또는 일반 텍스트로 처리할지 여부를 설정합니다. CDATA 보고는 기본적으로 비활성화되어 있습니다.
이 메서드는 범위가 지정된 XMLDocument2 - parseXML(String xmlDoc).
참조: 범위가 지정된 XMLNode - isCDATANode().
| 이름 | 유형 | 설명 |
|---|---|---|
| 사용 | 부울 | 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(부울 인식)
true로 설정하면 XMLDocument2 객체는 XML 네임스페이스를 사용하여 문서를 처리합니다.
이를 설정하지 않으면 네임스페이스가 있는 XML 문서가 올바르게 열거되지 않고 XPath 검색이 실패합니다.
| 이름 | 유형 | 설명 |
|---|---|---|
| 인식 | 부울 | true인 경우 XMLDocument2 객체는 XML 네임스페이스를 사용하여 문서를 처리합니다. |
| 유형 | 설명 |
|---|---|
| void |
범위가 지정된 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>