2014-12-16 2 views
1

필드 이름을 전달하여 필드 값을 가져 오려면 ISOMsg 개체를 반복하는 메서드를 구현 한 다음 전달 된 파일 이름과 일치하면 .my 요구 사항을 반환합니다 .xml 파일을 한 번 읽으면 다음 번에 해당 값을 검색하기 위해 필드 이름을 전달하여 해당 값을 검색합니다. config xml의 모든 필드.JPOS ISO8583 메시지 형식을 사용하여 필드 이름을 전달하여 필드 값을 검색하는 방법

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; 

    } 
    } 
} 

}

답변

-1

솔루션은 사용자 정의 mapper.here 모든 구성을 한 번 읽은 다음 이름으로 키 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(); 
} 

}

2

예. 필드 이름은 packager에서 packager까지 다를 수 있으므로 솔루션이 부서지기 쉽기 때문에 열거 형을 사용하거나 상수 만 사용하는 것이 좋습니다.

+0

그런 경우 숫자를 변경하려면 두 곳에서 변경해야합니다. 거기에 어떤 해결책이 있니? –

관련 문제