Editing / Remove XML Nodes in Server Side Scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-14-2019 04:58 AM
Hi All,
How can we edit / remove an XML node from an XML Document in Server side.
One of our application has a logic to set the data at once place and the data can be altered based on the case at another place.
We are using a scoped application:
For example, the following is the XML
<root>
<one>
<two>This is second text</two>
</one>
</root>
How can we change the text content inside <two> element.
var xml = new XMLDocument2 ();
xml.parseXML(xmlString);
var node = xml.getNode('/root/one/two');
What should be done after this to edit. It is also fine if the node two is deleted as well, so that a new node can be created in place.
Please help.
Expected Result is
<root>
<one>
<two>This is modified second text</two>
</one>
</root>
Thanks in Advance
Balu
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-16-2019 07:16 PM
Does it need to remain in XML format? My preferred method of handling XML objects is to convert it as soon as possible to a Javascript object/JSON.
var newObj = gs.xmlToJSON(stringOfXMLData);
newObj.root.one.two = 'updated second text';
gs.info(JSON.stringify(newObj,null,4)); //log pretty string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-17-2019 07:06 AM
Hi,
Thanks for you answer.
It should be in XML format. Well, the actual point is to have XML manipulated and use it somewhere else.
Example Scenario: Integration system data processing. Say from ServiceNow instance perspective
Service Requestor: A remote system using WebService. Say system-1.
Service Providors: Two different systems which again interacts through WebService. Say system-2 and system-3.
i) system-1 requests for something (in XML format) to ServiceNow.
ii) ServiceNow needs data from two different systems to process the request. So ServiceNow requests for data from system-2 and system-3.
iii) After getting the data, ServiceNow processes the data and sends the response back to system-1.
Now the requests to system-2 and system-3 have similar structure of request to ServiceNow from system-1. So XML is almost the same, only minor changes.
Having the same XML DOM manipulated and sent across will be more efficient, as DOM creation is a memory consuming task.
Also, imagine that in future instead of 2 systems that ServiceNow needs data from, there are 10 systems.
The only possible way to deal with this as I can see is to create new DOM, operate on it and then release reference after each request for garbage collector to do it's job.
Which is why I was wondering if there is any better way to do this.
About using
gs.xmlToJSON(stringOfXMLData);
This is not a safe way to operate on XML. I'd recommend to use XML DOM parsing methods (say XMLDocument2).
Reason is that I haven't get accurate result with this when there are attributes.
Try with the following XML and check the results.
<root attr-root="attributeRootValue">
<one attr-one="attributeOneValue">
<two>This is second text</two>
</one>
</root>
Thanks
Balu