You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Java映射EDIFACT文件求助:编码数据转指定格式

Hey there! Let's figure out how to map your EDIFACT input string to the desired UNH segment format using Java. Let's break this down step by step.

解决方案

First, I need to note that EDIFACT mappings depend heavily on your specific business rules—since you didn't share the exact logic linking UNBIATB11AMS135682300687X to UNH+1+SBRRES:09:3:1A', I'll cover both a simple hardcoded approach (if the output follows fixed rules) and a more flexible method using dedicated EDIFACT libraries.

1. Simple String Manipulation (For Fixed Rules)

If the output format is mostly fixed (e.g., the message reference 1 and message type SBRRES:09:3:1A are static, or you only need to extract specific parts from the input), a straightforward string builder will work:

public class EdifactUnhMapper {
    public static String convertToUnhSegment(String inputEdifact) {
        // Adjust these values based on your actual business rules
        String messageReference = "1";
        String messageTypeDetails = "SBRRES:09:3:1A";
        
        // Build the target UNH segment format
        return String.format("UNH+%s+%s'", messageReference, messageTypeDetails);
    }

    public static void main(String[] args) {
        String input = "UNBIATB11AMS135682300687X";
        String output = convertToUnhSegment(input);
        System.out.println(output); // Outputs: UNH+1+SBRRES:09:3:1A'
    }
}

If you need to dynamically extract values from the input (e.g., pulling a code from UNBIATB11AMS135682300687X), use Java's string methods like substring():

// Example: Extract the 6th and 7th characters (0-indexed positions 5-6)
String extractedCode = input.substring(5, 7); // Gets "11" from your input
// Map this extracted value to your target field as needed

2. Using Dedicated EDIFACT Libraries (For Complex Scenarios)

If you're working with full EDIFACT files (not just single segments), using a specialized library will save you time handling EDIFACT's structure and standards. Two popular options are Smooks EDIFACT and EDIFACT Parser.

Example with Smooks EDIFACT

  1. Add the Maven dependency to your project:
<dependency>
    <groupId>org.milyn</groupId>
    <artifactId>smooks-edifact</artifactId>
    <version>1.7.1</version>
</dependency>
  1. Parse the input EDIFACT exchange and build the UNH segment:
import org.milyn.edifact.EdifactParser;
import org.milyn.edifact.model.EdifactMessage;
import org.milyn.edifact.model.internal.Segment;

public class EdifactLibraryMapper {
    public static void main(String[] args) throws Exception {
        String inputEdifact = "UNBIATB11AMS135682300687X";
        
        // Parse the input EDIFACT string
        EdifactParser parser = new EdifactParser();
        EdifactMessage message = parser.parse(inputEdifact.getBytes());
        
        // Extract details from the input UNB segment (adjust field indices as per your EDIFACT version)
        Segment unbSegment = message.getSegments().get(0);
        String senderId = unbSegment.getFieldValue(2); // Example: Get sender ID from UNB
        
        // Build the target UNH segment
        Segment unhSegment = new Segment("UNH");
        unhSegment.addField("1"); // Message reference number
        unhSegment.addField("SBRRES:09:3:1A"); // Message type, version, release, association
        
        // Convert the segment to the desired string format
        String unhOutput = unhSegment.toString();
        System.out.println(unhOutput);
    }
}

Just remember to adjust field indices and mapping logic to match your specific EDIFACT implementation guide—different message types and versions have varying field positions.

内容的提问来源于stack exchange,提问作者Marwan Fahmi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:53:19