How to read XML file ?

Charlotte12
Tera Contributor

When a user uploads a file to a record, I'm unable to read the contents of an xml file without getting everything inside. Right now I'm able to use 【GlideSysAttachment】 and 【GlideTextReader】 to use the content stream. This enables me to get a string line by line of the xml document. However, it does not include the xml tags and anything inside the xml.

Example:
XML
<userdata>
<user>
<firstname>Belly3</firstname>
<lastname>Tripath</lastname>
</user>
<user>
<firstname>Belly1</firstname>
<lastname>Tripath</lastname>
</user>
<user>
<firstname>Belly2</firstname>
<lastname>Tripath</lastname>
</user>
</userdata>

Script

var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments('u_rpa_account',current.sys_id);

if (agr.next()) {
var attachmentContent = attachment.getContent(agr);
gs.addInfoMessage(attachmentContent+'====='+JSUtil.type_of(attachmentContent));
}

Using the above code, I getting "undefined and null". It is appearing in the line with "gs.addInfoMessage(attachmentContent+'====='+JSUtil.type_of(attachmentContent))".

Is there something wrong with how I'm setting this up?

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

so you want to read the XML file?

when you print the content does it show as XML string?

If yes then you can use XML parser for that XMLDocument class

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

are you in scoped application or global for your above script?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur, 

Good day. thanks for your quick response.

When I print the attachmentContent, I got nothing but "undefined"....

BTW, I'm in scoped application.

 

Any Advise Pls?

then you should add application cross scope privilege so that attachment can be read

Ideally it should automatically create these 2 entries; but if not then you create on your own

find_real_file.png

find_real_file.png

For me I am getting the XML file printed in background script in scoped app

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader