How do I convert data from an XML feed to JSON?

How do I convert data from an XML feed to JSON?

kprohaszkakprohaszka Posts: 15Questions: 3Answers: 0

I am importing data from an XML feed into my table. I am getting an error for an invalid JSON response. After looking into it, it seems I have to convert my XML data into json so it can be read, and I found the third example on this page but am not sure how to apply it so it works for me. I've been trying to get it to work with the expanding child rows to know avail, and any assistance would be much appreciated.

`function format(d) {
return (
'

' + '
Full name:
' + '
' + d.last + '
' + '
Extension number:
' + '
' + d.phone + '
' + '
Extra info:
' + '
And any further details here (images etc)...
' + '

'
);
}

    let table = new DataTable('#example', {
        // ajax: '../feeds/objects.txt',
        // columns: [
        //     {
        //         className: 'dt-control',
        //         orderable: false,
        //         data: null,
        //         defaultContent: ''
        //     },
        //     { data: 'name' },
        //     { data: 'position' },
        //     { data: 'office' },
        //     { data: 'salary' }
        // ],
        // order: [[1, 'asc']]
        ajax: {
            // dataType: "xml",
            url: '../feeds/employee_directory.xml',
            dataSrc: function (json) {
                for (var i = 0, ien = json.data.length; i < ien; i++) {
                    json.data[i][0] =
                        '<a href="/message/' + json.data[i][0] + '>View message</a>';
                }
                return json.data;
            }


        },
        columns: [
            {
                className: 'dt-control',
                orderable: false,
                data: null,
                defaultContent: ''
            },
            { data: 'last' },
            { data: 'first' },
            { data: 'phone' },
            { data: 'department' }
        ],
        order: [[1, 'asc']]
    });

    // Add event listener for opening and closing details
    table.on('click', 'td.dt-control', function (e) {
        let tr = e.target.closest('tr');
        let row = table.row(tr);

        if (row.child.isShown()) {
            // This row is already open - close it
            row.child.hide();
        }
        else {
            // Open this row
            row.child(format(row.data())).show();
        }
    });
</script>`

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,391Questions: 26Answers: 4,961
    edited November 6 Answer ✓

    The Ajax docs explain to use ajax.dataSrc to convert from XML to JSON. There isn't anything built into Datatables for this so you will need to write the code or use a prebuilt library. This SO thread might give you some places to look.

    Kevin

  • kprohaszkakprohaszka Posts: 15Questions: 3Answers: 0

    Thanks Kevin, I'll take a look at those!

Sign In or Register to comment.