【问题标题】:How to retrieve field values by passing field name using JPOS ISO8583 message format如何通过使用 JPOS ISO8583 消息格式传递字段名称来检索字段值
【发布时间】:2026-01-05 12:05:01
【问题描述】:

我想通过传递文件名来检索字段值。为了实现这一点,我已经实现了一个循环 ISOMsg 对象的方法,然后如果它找到与传递的文件名匹配,则返回。我的要求是读取 .xml 文件一次并使用该静态映射,然后在下一次通过传递字段名称检索相应的值以实现这一点,有一种方法可以检索配置 xml 中的所有字段。

protected static void getISO8583ValueByFieldName(ISOMsg isoMsg, String fieldName) {

for (int i = 1; i <= isoMsg.getMaxField(); i++) {

  if (isoMsg.hasField(i)) {

    if (isoMsg.getPackager().getFieldDescription(isoMsg, i).equalsIgnoreCase(fieldName)) {
      System.out.println(
          "    FOUND FIELD -" + i + " : " + isoMsg.getString(i) + " " + isoMsg.getPackager()
              .getFieldDescription(isoMsg, i));
       break;

    } 
  } 
} 

}

【问题讨论】:

    标签: java iso8583 jpos


    【解决方案1】:

    您还可以定义一个带有字段名称的枚举,映射到字段编号。 请注意,字段名称可能因打包程序而异,因此您的解决方案有点脆弱,最好使用枚举或仅使用常量。

    【讨论】:

    • 在这种情况下,如果我们想更改一个数字,我们必须在两个地方进行更改。有什么解决办法吗
    【解决方案2】:

    解决方案是实现一个自己的自定义映射器。这里是一个内存实现单例类,它会一次性读取所有配置,然后按名称提供 key id。

     /**
     * This is an memory implementation of singleton mapper class which contains ISO8583
     * field mappings as Key Value pairs [Field Name,Field Value]
     * in order
     *
     * Created on 12/16/2014.
     */
    public class ISO8583MapperFactory {
    
      private static Logger logger= Logger.getLogger(ISO8583MapperFactory.class);
    
      private static ISO8583MapperFactory instance = null;
      private static HashMap<String,Integer> fieldMapper=null;
    
    
    
      /**
       *
       * @throws IOException
       * @throws SAXException
       * @throws ParserConfigurationException
       */
      private ISO8583MapperFactory() throws IOException, SAXException, ParserConfigurationException {
        fieldMapper =new HashMap<String, Integer>();
        mapFields();
      }
    
      /**
       *
       * @throws ParserConfigurationException
       * @throws IOException
       * @throws SAXException
       */
      private void mapFields() throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    
    
        dBuilder.setEntityResolver(new GenericPackager.GenericEntityResolver());
        //InputStream in = new FileInputStream(ISO8583Constant.ISO8583_CONFIG_FILE_NAME);
    
        InputStream in =  getClass().getClassLoader().getResourceAsStream(ISO8583Constant.ISO8583_CONFIG_FILE_NAME);
        Document doc = dBuilder.parse(in);
    
    
        logger.info("Root element :" + doc.getDocumentElement().getNodeName());
    
        if (doc.hasChildNodes()) {
    
          loadNodes(doc.getChildNodes(), fieldMapper);
    
        }
      }
    
      /**
       *
       * @return
       * @throws ParserConfigurationException
       * @throws SAXException
       * @throws IOException
       */
      public static ISO8583MapperFactory getInstance()
          throws ParserConfigurationException, SAXException, IOException {
        if(instance==null){
          instance=new ISO8583MapperFactory();
        }
        return instance;
      }
    
      /**
       *
       * @param fieldName
       * @return
       */
      public Integer getFieldidByName(String fieldName){
        return fieldMapper.get(fieldName);
      }
    
    
    
      /**
       * Recursive method to read all the id and field name mappings
       * @param nodeList
       * @param fieldMapper
       */
      protected void loadNodes(NodeList nodeList,HashMap<String,Integer> fieldMapper) {
        logger.info(" Invoked loadNodes(NodeList nodeList,HashMap<String,Integer> fieldMapper)");
    
        for (int count = 0; count < nodeList.getLength(); count++) {
    
          Node tempNode = nodeList.item(count);
    
          // make sure it's element node.
          if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
    
            // get node name and value
            logger.info("\nNode Name =" + tempNode.getNodeName() + " [OPEN]");
            logger.info("Node Value =" + tempNode.getTextContent());
    
    
            if (tempNode.hasAttributes()) {
    
              // get attributes names and values
              NamedNodeMap nodeMap = tempNode.getAttributes();
    
              fieldMapper.put(nodeMap.getNamedItem(ISO8583Constant.FIELD_NAME).getNodeValue(),Integer.valueOf( nodeMap.getNamedItem(ISO8583Constant.FIELD_ID).getNodeValue()));
    
            }
    
            if (tempNode.hasChildNodes()) {
    
              // loop again if has child nodes
              loadNodes(tempNode.getChildNodes(), fieldMapper);
    
            }
            logger.info("Node Name =" + tempNode.getNodeName() + " [CLOSE]");
    
    
          }
    
        }
    
      }
    
      /**
       *
       * @return
       */
      public static HashMap<String, Integer> getFieldMapper() {
        return fieldMapper;
      }
    
      /**
       *
       * @param fieldMapper
       */
      public static void setFieldMapper(HashMap<String, Integer> fieldMapper) {
        ISO8583MapperFactory.fieldMapper = fieldMapper;
      }
    
    
    }
    

    例子

    public static void main(String[] args) throws IOException, ISOException, InterruptedException {
    try {
    
      ISO8583MapperFactory.getInstance().getFieldidByName("NAME");
    
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    } catch (SAXException e) {
      e.printStackTrace();
    }
    

    }

    【讨论】:

      最近更新 更多