How do I unpack an ISO 8583 message?

The code snippet below will show you how to unpack ISO 8583 message.

package org.kodejava.jpos;

import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.packager.GenericPackager;

import java.io.InputStream;

public class UnpackISOMessage {
    public static void main(String[] args) {
        UnpackISOMessage iso = new UnpackISOMessage();
        try {
            ISOMsg isoMsg = iso.parseISOMessage();
            iso.printISOMessage(isoMsg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private ISOMsg parseISOMessage() throws Exception {
        String message = "02003220000000808000000010000000001500120604120000000112340001840";
        System.out.printf("Message = %s%n", message);
        try {
            // Load package from resources directory.
            InputStream is = getClass().getResourceAsStream("/fields.xml");
            GenericPackager packager = new GenericPackager(is);
            ISOMsg isoMsg = new ISOMsg();
            isoMsg.setPackager(packager);
            isoMsg.unpack(message.getBytes());
            return isoMsg;
        } catch (ISOException e) {
            throw new Exception(e);
        }
    }

    private void printISOMessage(ISOMsg isoMsg) {
        try {
            System.out.printf("MTI = %s%n", isoMsg.getMTI());
            for (int i = 1; i <= isoMsg.getMaxField(); i++) {
                if (isoMsg.hasField(i)) {
                    System.out.printf("Field (%s) = %s%n", i, isoMsg.getString(i));
                }
            }
        } catch (ISOException e) {
            e.printStackTrace();
        }
    }
}

When you run the program you’ll get the following output:

Message = 02003220000000808000000010000000001500120604120000000112340001840
MTI = 0200
Field (3) = 000010
Field (4) = 000000001500
Field (7) = 1206041200
Field (11) = 000001
Field (41) = 12340001
Field (49) = 840

The xml packager (fields.xml) can be downloaded from the following link: fields.xml.

Maven Dependency

<dependency>
    <groupId>org.jpos</groupId>
    <artifactId>jpos</artifactId>
    <version>2.1.8</version>
</dependency>

Maven Central

Wayan

15 Comments

  1. Hi Pavan, was the error caused by the fields.xml could not be found? Instead of loading the packager file using getResourceAsStream() you can just pass the full path of the fields.xml to create an instance of GenericPackager.

    Change the following code:

    InputStream is = getClass().getResourceAsStream("/fields.xml");
    GenericPackager packager = new GenericPackager(is);
    

    To:

    String filename = "D:/jpos/fields.xml";
    GenericPackager packager = new GenericPackager(filename);
    
    Reply
  2. Hello Mr. I have the same error of malformed URL even when using the whole path. I wonder if there is any other dependency besides the jpos one? Thank you!

    Reply
  3. Hi, I am getting this error what do I do now ?

    Message = 02003220000000808000000010000000001500120604120000000112340001840
    java.lang.Exception: org.jpos.iso.ISOException: Error reading E:/iso.xml (org.xml.sax.SAXParseException; systemId: file:///E:/iso.xml; lineNumber: 1; columnNumber: 13; Document is invalid: no grammar found.)
        at jpos.UnpackISOMessage.parseISOMessage(UnpackISOMessage.java:33)
        at jpos.UnpackISOMessage.main(UnpackISOMessage.java:14)
    Caused by: org.jpos.iso.ISOException: Error reading E:/iso.xml (org.xml.sax.SAXParseException; systemId: file:///E:/iso.xml; lineNumber: 1; columnNumber: 13; Document is invalid: no grammar found.)
        at org.jpos.iso.packager.GenericPackager.readFile(GenericPackager.java:204)
        at org.jpos.iso.packager.GenericPackager.(GenericPackager.java:120)
        at jpos.UnpackISOMessage.parseISOMessage(UnpackISOMessage.java:27)
        ... 1 more
    
    Reply
  4. Can we parse and split multiple records which are on single line? like ISO packaged file contains all its records on single line. so by default it will unpack only first MTI record only.

    Reply
  5. Why this code doesn’t gives us Bitmap values as well? What changes will be needed if we want Bitmap fields too from ISO 8583 message.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.