Java 读取 idml 文件中的内容

1 篇文章 0 订阅
public static void main(String[] args) throws IOException {
        String fileZip = "idml文件路径";
        File destDir = new File("idml解压后文件位置");
        // 字节流
        byte[] buffer = new byte[1024];
        // 解压idml文件
        ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
        ZipEntry zipEntry = zis.getNextEntry();
        while (zipEntry != null) {
            // 将解压后的文件放入到指定的文件夹下
            File newFile = newFile(destDir, zipEntry);
            if (!newFile.getParentFile().exists()){
                newFile.getParentFile().mkdirs();
            }
            // 写入数据到文件中
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            zipEntry = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();

        // 文件夹名称
        String fileDir = destDir.getAbsolutePath();

        // 读取xml文件
        try {
            SAXReader reader = new SAXReader();
            Document read = reader.read(new File(fileDir + "\\designmap.xml"));
            Element element = read.getRootElement();
            Iterator<Element> it = element.elementIterator("Story");
            while (it.hasNext()) {
                Element next = it.next();
                for (Attribute attribute : next.attributes()) {
                    getContent(fileDir + "\\" + attribute.getValue());
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }

    /**
     * 读取指定的xml文件
     * @param contentPath xml文件路径
     * @throws DocumentException
     */
    private static void getContent(String contentPath) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document read = reader.read(new File(contentPath));
        Element element = read.getRootElement();
        Iterator<Element> elementIterator = element.elementIterator("Story");
        while (elementIterator.hasNext()) {
            Iterator<Element> ParagraphStyleRange = elementIterator.next().elementIterator("ParagraphStyleRange");
            while (ParagraphStyleRange.hasNext()){
                Iterator<Element> characterStyleRange = ParagraphStyleRange.next().elementIterator("CharacterStyleRange");
                while (characterStyleRange.hasNext()){
                    Iterator<Element> content = characterStyleRange.next().elementIterator("Content");
                    while (content.hasNext()){
                        System.out.println(content.next().getText());
                    }
                }
            }
        }
    }

    /**
     * 将解压后的文件放到指定的文件夹中
     * @param destinationDir 文件解压后所放的位置
     * @param zipEntry  解压后的文件
     * @return
     * @throws IOException
     */
    public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
        File destFile = new File(destinationDir, zipEntry.getName());

        String destDirPath = destinationDir.getCanonicalPath();
        String destFilePath = destFile.getCanonicalPath();

        if (!destFilePath.startsWith(destDirPath + File.separator)) {
            throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
        }

        return destFile;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值