Java处理EDI解析的测试类
时间: 2024-06-08 20:06:07 浏览: 145
以下是一个简单的Java测试类,用于解析EDI文件:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.milyn.edi.unedifact.d96a.D96AInterchangeFactory;
import org.milyn.edi.unedifact.d96a.INVOic;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup11;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup12;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup13;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup14;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup15;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup16;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup17;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup18;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup19;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup1a;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup1a.INVOicSegmentGroup1a1;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup1a.INVOicSegmentGroup1a2;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup1a.INVOicSegmentGroup1a3;
import org.milyn.edi.unedifact.d96a.INVOic.INVOicSegmentGroup.INVOicSegmentGroup1.INVOicSegmentGroup1a.INVOicSegmentGroup1a4;
import org.milyn.smooks.edi.unedifact.model.r41.SegmentGroup;
import org.milyn.smooks.edi.unedifact.model.r41.SegmentGroup10;
public class EdiParserTest {
public static void main(String[] args) throws IOException {
String ediFile = "example.edi"; // EDI文件路径
BufferedReader reader = new BufferedReader(new FileReader(ediFile));
String ediContent = "";
String line;
while ((line = reader.readLine()) != null) {
ediContent += line;
}
reader.close();
D96AInterchangeFactory factory = D96AInterchangeFactory.getInstance();
INVOic invoice = factory.fromUNEdifact(ediContent, INVOic.class);
// 获取INVOIC头部信息
INVOicSegmentGroup1 invoiceHeader = invoice.getINVOicSegmentGroup1();
System.out.println("Header Information:");
System.out.println("Invoice Number: " + invoiceHeader.getE1004());
System.out.println("Invoice Date: " + invoiceHeader.getE1373());
System.out.println("Invoice Currency Code: " + invoiceHeader.getE6345());
// 获取INVOIC行项目信息
List<INVOicSegmentGroup> invoiceItems = invoice.getINVOicSegmentGroup();
System.out.println("Line Item Information:");
for (INVOicSegmentGroup item : invoiceItems) {
INVOicSegmentGroup1a itemHeader = item.getINVOicSegmentGroup1().getINVOicSegmentGroup1a();
INVOicSegmentGroup11 itemDetail = item.getINVOicSegmentGroup11();
System.out.println("Item Number: " + itemHeader.getE7140());
System.out.println("Product Code: " + itemDetail.getE7143());
System.out.println("Product Description: " + itemDetail.getE7008());
System.out.println("Invoice Quantity: " + itemDetail.getE6063());
System.out.println("Invoice Unit Price: " + itemDetail.getE6411());
System.out.println("Invoice Line Total: " + itemDetail.getE5463());
}
}
}
```
这个测试类使用了Milyn EDI库来解析EDI文件。你需要在项目中添加以下Maven依赖:
```xml
<dependency>
<groupId>org.milyn</groupId>
<artifactId>milyn-edisax-unedifact</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.milyn</groupId>
<artifactId>milyn-smooks-core</artifactId>
<version>1.7.1</version>
</dependency>
```
请注意,这个测试类仅适用于特定类型的EDI文件。如果你的EDI文件结构不同,你需要根据你自己的情况编写代码。
阅读全文