Processing XML SOAP Response Data in Zapier using Node.js

I’m working with a SOAP XML response that contains user information including email, display name, status, and session time. The XML structure looks like this:

<result>
    <headers>
        <items>
            <field>Email</field>
            <field>Display Name</field>
            <field>Status</field>
            <field>Session Time</field>
        </items>
    </headers>
    <entries>
        <items>
            <field>[email protected]</field>
            <field>Admin User</field>
            <field>Offline</field>
            <field>245</field>
        </items>
    </entries>
    <entries>
        <items>
            <field>[email protected]</field>
            <field>Manager User</field>
            <field>Online</field>
            <field>120</field>
        </items>
    </entries>
    <entries>
        <items>
            <field>[email protected]</field>
            <field>Guest User</field>
            <field>Offline</field>
            <field>87</field>
        </items>
    </entries>
</result>

Right now I can count offline users with this code:

var offlineCount = inputData.responseData.split("Offline").length;
output = [{offlineCount: offlineCount}]

This returns offlineCount: 2

I need help extracting the names of offline users and calculating the total session time for all users. I want the output to show:

  • offlineCount: 2
  • userNames: Admin User, Guest User
  • totalSessionTime: 452 (245 + 120 + 87)

What’s the best way to parse this XML and extract this information?

Regex is probably easier here since Zapier doesn’t play nice with external libraries. Try var matches = inputData.responseData.match(/<field>([^<]+)<\/field>/g); then group every 4 matches into one user record. Works well for structured XML like this without needing imports.

Just hit this same issue last month. DOMParser worked way better for me in Zapier - it’s built-in and doesn’t need external dependencies. Here’s what I used:

const parser = new DOMParser();
const xmlDoc = parser.parseFromString(inputData.responseData, 'text/xml');

const entries = xmlDoc.getElementsByTagName('entries');
let offlineUsers = [];
let totalSessionTime = 0;

for (let i = 0; i < entries.length; i++) {
    const fields = entries[i].getElementsByTagName('field');
    const displayName = fields[1].textContent;
    const status = fields[2].textContent;
    const sessionTime = parseInt(fields[3].textContent);
    
    if (status === 'Offline') {
        offlineUsers.push(displayName);
    }
    totalSessionTime += sessionTime;
}

output = [{
    offlineCount: offlineUsers.length,
    userNames: offlineUsers.join(', '),
    totalSessionTime: totalSessionTime
}];

Skips the xml2js dependency mess and handles the XML structure fine. Just use textContent to grab clean field values without the XML tags.

I’ve hit this same XML parsing issue in Zapier. Your string splitting method won’t work right because it’s not actually parsing the XML structure - just doing basic string matching. You need a real XML parser like xml2js or convert it to JSON first.

Here’s what I used for a similar problem:

const xml2js = require('xml2js');

xml2js.parseString(inputData.responseData, (err, result) => {
    const headers = result.result.headers[0].items[0].field;
    const entries = result.result.entries;
    
    let offlineUsers = [];
    let totalSessionTime = 0;
    
    entries.forEach(entry => {
        const fields = entry.items[0].field;
        const status = fields[2];
        const displayName = fields[1];
        const sessionTime = parseInt(fields[3]);
        
        if (status === 'Offline') {
            offlineUsers.push(displayName);
        }
        totalSessionTime += sessionTime;
    });
    
    output = [{
        offlineCount: offlineUsers.length,
        userNames: offlineUsers.join(', '),
        totalSessionTime: totalSessionTime
    }];
});

This way you’re actually mapping header positions to data fields and handling the XML structure correctly instead of just matching strings.