XMLDocument 스크립트 객체
XML 문서에서 XML 데이터를 구문 분석하고 추출하기 위한 JavaScript 객체 래퍼(문자열)입니다.
이 Javascript 클래스를 사용하여 XML 문자열(일반적으로 웹 서비스 호출의 반환 값 또는 ECC 큐의 XML 페이로드)에서 객체를 인스턴스화할 수 있습니다. Javascript 비즈니스 규칙에서 XMLDocument 객체를 사용하면 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 XMLDocument(xmlString);네임스페이스로 규정된 XML 문자열의 XML 구문 분석을 수행하려면 XMLDocument 생성자의 두 번째 인수에 대해 "true"를 지정하십시오. 다음은 해당 요소의 네임스페이스 한정을 포함하는 구문 분석 및 XML 문자열의 예입니다.
var xmlString = "<bk:book xmlns:bk='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'>" +
"<bk:title>Cheaper by the Dozen</bk:title>" +
"<isbn:number>1568491379</isbn:number>" +
"</bk:book>";
var xmldoc = new XMLDocument(xmlString, true); // XML document is name space aware
노드 및 요소 찾기
이제 XMLDocument 객체가 있으므로 다음 API를 호출하여 XML 문서의 노드와 요소를 찾고 텍스트를 직접 추출할 수 있습니다. 노드 및 특성을 찾기 위한 쿼리 구문은 XPath를 기반으로 합니다.
주:
이 객체와의 XML 구문 분석은 네임스페이스를 인식하지 않습니다. 이는 네임스페이스가 있는 노드 이름을 찾는 경우(예: 다음을 의미합니다. "<이름:myelement> ... ", XPath 검색 문자열은 "//myelement"가 됩니다.
다음은 XPath로 노드를 찾고 결과 노드에서 텍스트 값을 가져오는 예제입니다.
var str = xmldoc.getNodeText("//two"); // returns the first occurrence of the node
// str == "abcd1234"
str = xmldoc.getNodeText("//three");
// str == "1234abcd"
str = xmldoc.getNodeText("/test/one/*");
// str == "abcd1234"
str = xmldoc.getNodeInt("//number");
// str == 1234
다음 예시에서는 XPath로 노드를 찾고 노드 및 요소의 API를 사용하여 속성과 값을 가져옵니다.
var node = xmldoc.getNode("/test/one/two");
// node.getNodeName() == "two"
// node.getNodeType() == "1" // 1 == ELEMENT_NODE
// node.getLastChild().getNodeType() == "3" // 3 == TEXT_NODE
// node.getLastChild().getNodeValue() == "abcd1234"또는 다음과 같은 편의 함수를 사용하여 노드 이름과 유형을 가져올 수 있습니다
str = xmldoc.getNodeName("//three");
// str == "three"
str = xmldoc.getNodeType("//three");
// str == "1"위치별로 직접 반복하거나 액세스할 수 있는 노드 목록을 가져올 수도 있습니다
var nodelist = xmldoc.getNodes("//one/*"); // two, three, and two
// nodelist.getLength() == "3"
// nodelist.item(0).getNodeName() == "two"
// nodelist.item(1).getNodeName() == "three"다음은 네임스페이스로 정형화된 XML 문자열을 구문 분석하는 예제입니다.
var xmlString = "<bk:book xmlns:bk='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'>" +
"<bk:title>Cheaper by the Dozen</bk:title>" +
"<isbn:number>1568491379</isbn:number>" +
"</bk:book>";
var xmldoc = new XMLDocument(xmlString, true);
var str = xmldoc.getNodeText("//bk:title"); // returns the first occurence of the node
gs.log(str);
str = xmldoc.getNodeText("/bk:book/*");
gs.log(str);
str = xmldoc.getNodeInt("//isbn:number");
gs.log(str);속성 값 가져오기
속성은 노드의 확장일 뿐이므로 모두 동일한 API를 갖습니다.
다음 예제에서는 노드를 쿼리하여 위치별로 해당 속성을 가져오는 방법을 보여 줍니다
node = xmldoc.getNode("//two");
// node.getAttributes().item(0).getNodeValue() == "xxx"
str = xmldoc.getAttribute("//two", "att");
// str == "xxx"XPath에는 속성 노드를 직접 찾기 위한 쿼리 구문도 있습니다. 예를 들어
str = xmldoc.getNodeText("//*[@att=\"yyy\"]");
// str == "1234abcd"
str = xmldoc.getNode("//@boo").getNodeValue();
// str == "yah"
새 요소 만들기
XML 요소는 일단 만들어진 XML 문서의 모든 수준에서 만들 수 있습니다. setCurrent 함수를 사용하여 새 요소가 자식 요소로 생성될 위치를 지정하고 createElement 함수를 사용하여 요소를 생성합니다.
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 XMLDocument(xmlString);
xmldoc.createElement("new", "test"); // creates the new element at the document element level if setCurrent is never called
xmldoc.createElement("new2"); // calling without a value will create a new element by itself
var el = xmldoc.createElement("new3");
xmldoc.setCurrent(el); // this is now the parent of any new elements created subsequently using createElement()
xmldoc.createElement("newChild", "test");결과 XML 문서는 다음과 같습니다
<test>
<one>
<two att="xxx">abcd1234</two>
<three boo="yah" att="yyy">1234abcd</three>
<two>another</two>
</one>
<number>1234</number>
<new>test<new>
<new2/>
<new3>
<newChild>test</newChild>
</new3>
</test>