{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":620,"path":"api-reference/scripts/c_XMLDocumentScriptObject.md","title":"XMLDocument script object","bundle":null,"doc_type":null,"release":"australia","body":"\n# XMLDocument script object\n\nA JavaScript object wrapper for parsing and extracting XML data from an XML document \\(String\\).\n\nUse this Javascript class to instantiate an object from an XML string, usually a return value from a Web Service invocation, or the XML payload of ECC Queue. Using the XMLDocument object in a Javascript business rule lets you query values from the XML elements and attributes directly.\n\n## Constructor\n\nThe constructor of a script object creates a new instance of the object to be used.\n\n```\nvar xmlString = \"<test>\" +\n                \"  <one>\" +\n                \"    <two att=\\\"xxx\\\">abcd1234</two>\" +\n                \"    <three boo=\\\"yah\\\" att=\\\"yyy\\\">1234abcd</three>\" +\n                \"    <two>another</two>\" +\n                \"  </one>\" +\n                \"  <number>1234</number>\" +\n                \"</test>\";\n\nvar xmldoc = new XMLDocument(xmlString);\n```\n\nTo perform XML parsing of an XML string that is name space qualified, specify \"true\" for the second argument for the XMLDocument constructor. The following is an example of parsing and XML string that contains name space qualification of its elements.\n\n```\nvar xmlString = \"<bk:book xmlns:bk='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'>\" +\n                  \"<bk:title>Cheaper by the Dozen</bk:title>\" +\n                  \"<isbn:number>1568491379</isbn:number>\" +\n                \"</bk:book>\";\n\nvar xmldoc = new XMLDocument(xmlString, true); // XML document is name space aware\n\n```\n\n## Locating nodes and elements\n\nNow that we have the XMLDocument object, we can call the following APIs to locate nodes and elements of the XML document, as well as extract text from it directly. The query syntax for locating nodes and attributes is based on XPath.\n\n**Note:** XML parsing with this object is not namespace aware, this means that if you are locating a node name with namespace in it eg. \"&lt;names:myelement&gt; ...\", the XPath search string would be \"//myelement\"\n\nThe following are examples of locating a node by its XPath and getting the text value out of the resulting node.\n\n```\nvar str = xmldoc.getNodeText(\"//two\"); // returns the first occurrence of the node\n// str == \"abcd1234\"\n\nstr = xmldoc.getNodeText(\"//three\");\n// str == \"1234abcd\"\n\nstr = xmldoc.getNodeText(\"/test/one/*\");\n// str == \"abcd1234\"\n\nstr = xmldoc.getNodeInt(\"//number\");\n// str == 1234\n\n```\n\nThe following examples locates the node by XPath and uses the API on node and element to get attributes and value.\n\n```\nvar node = xmldoc.getNode(\"/test/one/two\");\n// node.getNodeName() == \"two\"\n// node.getNodeType() == \"1\" // 1 == ELEMENT_NODE\n// node.getLastChild().getNodeType() == \"3\" // 3 == TEXT_NODE\n// node.getLastChild().getNodeValue() == \"abcd1234\"\n```\n\nOr you can use the following convenience functions to get the node name and type\n\n```\nstr = xmldoc.getNodeName(\"//three\");\n// str == \"three\"\n\nstr = xmldoc.getNodeType(\"//three\");\n// str == \"1\"\n```\n\nYou can also get a list of nodes that you can iterate or access directly by position\n\n```\nvar nodelist = xmldoc.getNodes(\"//one/*\"); // two, three, and two\n// nodelist.getLength() == \"3\"\n// nodelist.item(0).getNodeName() == \"two\"\n// nodelist.item(1).getNodeName() == \"three\"\n```\n\nThe following is an example of parsing an XML string that is qualifed with name spaces.\n\n```\nvar xmlString = \"<bk:book xmlns:bk='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'>\" +\n                  \"<bk:title>Cheaper by the Dozen</bk:title>\" +\n                  \"<isbn:number>1568491379</isbn:number>\" +\n                \"</bk:book>\";\n\nvar xmldoc = new XMLDocument(xmlString, true);\nvar str = xmldoc.getNodeText(\"//bk:title\"); // returns the first occurence of the node\ngs.log(str);\n\nstr = xmldoc.getNodeText(\"/bk:book/*\");\ngs.log(str);\n\nstr = xmldoc.getNodeInt(\"//isbn:number\");\ngs.log(str);\n```\n\n## Getting attribute values\n\nAn attribute is just an extension of a node and so it has all the same APIs.\n\nThe following example shows how to query for a node to get its attribute by position\n\n```\nnode = xmldoc.getNode(\"//two\");\n// node.getAttributes().item(0).getNodeValue() == \"xxx\"\n\nstr = xmldoc.getAttribute(\"//two\", \"att\");\n// str == \"xxx\"\n```\n\nXPath also has a query syntax for locating the attribute node directly, for example\n\n```\nstr = xmldoc.getNodeText(\"//*[@att=\\\"yyy\\\"]\");\n// str == \"1234abcd\"\n\nstr = xmldoc.getNode(\"//@boo\").getNodeValue();\n// str == \"yah\"\n\n```\n\n## Creating new elements\n\nAn XML element can be created at any level of the XML document once it has been created. Use the setCurrent function to position where the new element will be created as a child element, and use the createElement function to create the element.\n\n```\nvar xmlString = \"<test>\" +\n                \"  <one>\" +\n                \"    <two att=\\\"xxx\\\">abcd1234</two>\" +\n                \"    <three boo=\\\"yah\\\" att=\\\"yyy\\\">1234abcd</three>\" +\n                \"    <two>another</two>\" +\n                \"  </one>\" +\n                \"  <number>1234</number>\" +\n                \"</test>\";\n\nvar xmldoc = new XMLDocument(xmlString);\nxmldoc.createElement(\"new\", \"test\"); // creates the new element at the document element level if setCurrent is never called\nxmldoc.createElement(\"new2\"); // calling without a value will create a new element by itself\n\nvar el = xmldoc.createElement(\"new3\");\nxmldoc.setCurrent(el); // this is now the parent of any new elements created subsequently using createElement()\nxmldoc.createElement(\"newChild\", \"test\");\n```\n\nThe resulting XML document looks like this\n\n```\n<test>\n  <one>\n    <two att=\"xxx\">abcd1234</two>\n    <three boo=\"yah\" att=\"yyy\">1234abcd</three>\n    <two>another</two>\n  </one>\n  <number>1234</number>\n  <new>test<new>\n  <new2/>\n  <new3>\n      <newChild>test</newChild>\n  </new3>\n</test>\n```\n\n-   **[XMLHelper](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/c_XMLHelper.md)**  \nThe XML helper script include makes it easy to parse XML in scripts.\n\n**Parent Topic:**[Server-side scripting](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/c_ServerScripting.md)\n\n"}}