java解析java文件 (java解析epub)

1、需求:Java解析ePub电子书,获取电子书目录

java解析doc,java解析form

2、准备:

java解析doc,java解析form

项目需要jar包

3、新建java项目:test_epub

java解析doc,java解析form

项目目录

java解析doc,java解析form

4、代码实现:

Test.java

package com.hk;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

import nl.siegmann.epublib.domain.Book;

import nl.siegmann.epublib.domain.Metadata;

import nl.siegmann.epublib.domain.Resource;

import nl.siegmann.epublib.domain.Spine;

import nl.siegmann.epublib.domain.SpineReference;

import nl.siegmann.epublib.domain.TOCReference;

import nl.siegmann.epublib.domain.TableOfContents;

import nl.siegmann.epublib.epub.EpubReader;

public class Test {

public static void main(String[] args) {

System.out.println("hello world");

EpubReader epubReader = new EpubReader();

//处理io流路径

String currentPath = Thread.currentThread().getClass().getResource("/").toString();

// String epubPath = currentPath + "epub/myBook.epub";

String epubPath = currentPath + "epub/test.epub";

epubPath = epubPath.substring(6, epubPath.length());

epubPath = epubPath.replace("/", "/");

System.out.println(epubPath);

//读epub文件

Book book = null;

try {

InputStream inputStr = new FileInputStream(epubPath);

book = epubReader.readEpub(inputStr);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

//设置epub文件内title.

//本处修改了toc.ncx文件中的<docTitle>和content.opf中的<dc:title>标签内容.

List<String> titlesList = new ArrayList<String>();

titlesList.add("test book");

book.getMetadata().setTitles(titlesList);

System.out.println(book.getCoverImage());//封面图

Metadata md=book.getMetadata();//书籍的头信息。比如,作者,出版社,语言等

System.out.println("标题:"+md.getFirstTitle());

System.out.println("格式:"+md.getFormat());

System.out.println("语言:"+md.getLanguage());

System.out.println("作者:"+md.getAuthors().toString());

System.out.println("贡献者:"+md.getContributors().toString());

System.out.println("出版日期:"+md.getDates().toString());

System.out.println("描述:"+md.getDescriptions().toString());

System.out.println("标识:"+md.getIdentifiers().toString());

System.out.println("发布者:"+md.getPublishers().toString());

System.out.println("版权:"+md.getRights().toString());

System.out.println("学科:"+md.getSubjects().toString());

System.out.println("标题:"+md.getTitles().toString());

System.out.println("类型:"+md.getTypes().toString());

//解析一级目录

TableOfContents ts=book.getTableOfContents();

List<TOCReference> toc=ts.getTocReferences();

for(TOCReference to:toc){

System.out.println("一级目录:"+to.getTitle());

//解析二级目录

List<TOCReference> toc1=to.getChildren();

for(TOCReference to1:toc1){

System.out.println(" 二级目录:"+to1.getTitle());

}

}

//spine

Spine spine=book.getSpine();

List<SpineReference> list= spine.getSpineReferences();

System.out.println("-------------spine-------------");

for(SpineReference srf:list){

System.out.println(srf.getResource().toString());

}

List<Resource> re=ts.getAllUniqueResources();

for(Resource res:re){

//System.out.println(res);

}

//write epub

//EpubWriter epubWriter = new EpubWriter();

//try {

//OutputStream ouput = new FileOutputStream("mynewbook.epub");

//epubWriter.write(book, ouput);

//} catch (FileNotFoundException e) {

//e.printStackTrace();

//} catch (IOException e) {

//e.printStackTrace();

//}

}

}

5、输出结果

java解析doc,java解析form

输出结果

java解析doc,java解析form

6、简单实现功能,需要其他功能,可以查看接口文档。如需源码,可加关注私聊我。