Import XML file info

chandukollapart
Tera Contributor

Hi,

 

We are receiving large xml file as a attachment through API and we are using saveBodyAsAttachment method because it has lot of info. So once the file is received we want to import the data. I tried data source and defined the Xpath but the problem is we have values that needs to import from different nodes. Attached sample image of XML. We like to import Policy_ID and Total values

 

So looking for options to import the file either can we define multiple xpath or get values directly from file

chandukollapart_0-1737883026874.png

 

Any suggestions. Thank you.

 

7 REPLIES 7

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @chandukollapart - Try setting Xpath for each row to /COMPLIANCE_POLICY_REPORT on the data source, then you should be able to extract POLICY_ID from <FILTERS> node and TOTAL from <CONTROL_INSTANCES> node in your transform map.

Hi @Sheldon  Swift 

 

I tried that but I am getting error because of size/info of the file.

 

 

What's the error? I created a sample file of the same structure (sample.xml) and then created a table transform map with the following script:

 

(function transformRow(source, target, map, log, isUpdate) {

    try {
        // Extract XML from the source table fields
        var headerXml = source.u_header; // Replace with the actual field name for the header XML
        var controlInstancesXml = source.u_control_instances; // Replace with the actual field name for the control instances XML

        // Parse the XML for <POLICY_ID> from <FILTERS>
        if (headerXml) {
            var headerDoc = new XMLDocument2();
            headerDoc.parseXML(headerXml);
            var policyIdNode = headerDoc.getNode('/HEADER/FILTERS/POLICY_ID');
            if (policyIdNode) {
                var policyId = policyIdNode.getTextContent();
                target.u_policy_id = policyId;
            } else {
                // POLICY_ID node not found in HEADER XML
            }
        } else {
            // HEADER XML is empty or missing
        }

        // Parse the XML for <TOTAL> from <CONTROL_INSTANCES>
        if (controlInstancesXml) {
            var controlDoc = new XMLDocument2();
            controlDoc.parseXML(controlInstancesXml);
            var totalNode = controlDoc.getNode('/CONTROL_INSTANCES/TOTAL');
            if (totalNode) {
                var total = totalNode.getTextContent();
                target.u_policy_total = total;
            } else {
                // TOTAL node not found in CONTROL_INSTANCES XML
            }
        } else {
            // CONTROL_INSTANCES XML is empty or missing
        }
    } catch (e) {
        log.error('Error during Transform Map script execution: ' + e.message);
    }

})(source, target, map, log, action === "update");

 

 

 

Hi @Sheldon  Swift 

 

Thank you for the response. Below is the error and I saw this KB related to that error. "om.glide.db.impex.datasource.DataSourceException: Incorrect or missing XPath expression"

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0832837

 

So to run that transform script file has to load completely right?