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

Hi Ankur, 

Good day.

The script is written in the business role. I don't think this is the reason.
Is there any other possibility?
Sorry,I just find I'm in global.

Hi,

then for global I used this script and it gave me the attachment content

I assume you have attached the correct file i.e. an xml file to record

var sysIdOfRecord = 'f9d7c729079801102011ff208c1ed025';

var grAttachment = new GlideRecord('sys_attachment');

grAttachment.get('table_sys_id', sysIdOfRecord);

var ga = new GlideSysAttachment();

var base64EncodedData = GlideBase64.encode(ga.getBytes(grAttachment));

var data = GlideStringUtil.base64Decode(base64EncodedData);

gs.info("Information Available in File: " + data);

find_real_file.png

Regards
Ankur

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

Hi,
Using the code you gave,

var grAttachment = new GlideRecord('sys_attachment');

grAttachment.get('table_sys_id', current.sys_id);

var ga = new GlideSysAttachment();

var base64EncodedData = GlideBase64.encode(ga.getBytes(grAttachment));

var data = GlideStringUtil.base64Decode(base64EncodedData);

gs.addInfoMessage("Information Available in File: " + data);

This enables me to get a anything inside the xml.
However, it does not include the xml tags.


Hi,

is it printing the XML content within that .xml file?

Once you get that XML you need to use XML Document to parse it

Regards
Ankur

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