java使用dom4j读取ofd发票的发票号码

依赖

<dependency>
  <groupId>dom4j</groupId>
  <artifactId>dom4j</artifactId>
  <version>1.6.1</version>
</dependency>

代码

package com.dl.dml.business.test.test;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.util.StreamUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

/**
 * @projectName: dml_frame
 * @package: com.dl.dml.business.test.test
 * @className: MainTest
 * @author: liuwenpu
 * @description: 解析ofd发票读取发票号码
 * @date: 2024/7/10 17:11
 * @version: 1.0
 */
public class MainTest {

    public static void main(String[] args) throws DocumentException {
        String path = "C:\\Users\\Lenovo\\Desktop\\02xxxx\\xxx.ofd";
        File file = new File(path);
        System.out.println(getFphm(parseOfd(file)));
    }

    /**
     * 解析ofd为xml文件
     * @param file
     * @return
     * @throws DocumentException
     */
    public static Element parseOfd(File file) throws DocumentException {
        String body;
        try (ZipFile zipFile = new ZipFile(file)) {
            //Doc_0/Pages/Page_0/Content.xml 该路径内容更全面
            ZipEntry entry = zipFile.getEntry("OFD.xml");
            InputStream input = zipFile.getInputStream(entry);
            body = StreamUtils.copyToString(input, StandardCharsets.UTF_8);
        } catch (ZipException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Document document = DocumentHelper.parseText(body);
        return document.getRootElement();
    }

    /**
     * 获取发票号码
     * @param rootElement
     * @return
     */
    public static String getFphm(Element rootElement) {
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            if("DocBody".equals(element.getName())) {
                //获取DocBody下所有标签
                List<Element> docBodyList = element.elements();
                for (Element docBodyEle : docBodyList) {
                    if("DocInfo".equals(docBodyEle.getName())) {
                        //获取DocInfo下所有标签
                        List<Element> docInfoList = docBodyEle.elements();
                        for (Element docInfoEle : docInfoList) {
                            if("CustomDatas".equals(docInfoEle.getName())) {
                                List<Element> customDataElement = docInfoEle.elements();
                                for (Element customDataEle : customDataElement) {
                                    if("发票号码".equals(customDataEle.attributeValue("Name"))) {
                                        return customDataEle.getStringValue();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

}

posted @ 2024-07-10 17:42  幸运刘  阅读(58)  评论(0编辑  收藏  举报