- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2023 12:05 AM - edited ‎10-25-2023 12:23 AM
Hello Everyone,
I am stuck in a problem in which I want to print only a single element in code.
The code as follow:-
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
Any help will be greatful
Regards,
Suyash
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2023 12:31 AM - edited ‎10-25-2023 12:32 AM
You're going to need to use XMLDocument2 | ServiceNow Developers
Something like
/*
* Retrieves the attribute value from the given XML.
* Params:
* xmlDoc - XMLDocument2, XML document to parse
* Returns:
* String, attribute value or null if not found
*/
function getAttributeKey (xmlDoc) {
var node = xmlDoc.getNode("/test/one/two");
if (node != null) {
return node.getAttribute("att");
} else {
return null;
}
}
// Example usage:
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 attribute = getAttributeKey(xmlDoc);
if (attribute != null) {
gs.info("The attribute value is: " + attribute);
} else {
gs.info("The attribute value is not found.");
}
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2023 12:12 AM
Can you please explain what exactly you're trying to accomplish?
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2023 12:16 AM
Hello,
I want to print xxx in the line " <two att=\"xxx\">abcd1234</two>"+
and yah or yyy in the given string " <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
like some element in the xml string.
How can I get the desired output?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2023 12:31 AM - edited ‎10-25-2023 12:32 AM
You're going to need to use XMLDocument2 | ServiceNow Developers
Something like
/*
* Retrieves the attribute value from the given XML.
* Params:
* xmlDoc - XMLDocument2, XML document to parse
* Returns:
* String, attribute value or null if not found
*/
function getAttributeKey (xmlDoc) {
var node = xmlDoc.getNode("/test/one/two");
if (node != null) {
return node.getAttribute("att");
} else {
return null;
}
}
// Example usage:
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 attribute = getAttributeKey(xmlDoc);
if (attribute != null) {
gs.info("The attribute value is: " + attribute);
} else {
gs.info("The attribute value is not found.");
}
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2023 12:37 AM
What's the business requirement here?
You can use XML parsing or convert that XML to JSON and then parse the JSON
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader