OpenDocument(.odt)は、実際には複数のxmlファイルを含むzipパッケージです。 Content.xmlには、ドキュメントの実際のテキストコンテンツが含まれています。見出しには興味があり、テキストの中にはhタグがあります。 ODTについてもっと読む。
QueryPathで.odtファイルから見出しを抽出する実装が見つかりました。
元の質問はJavaに関するものだったので、ここにあります。まず、ZipFileを使ってcontent.xmlにアクセスする必要があります。次に、SAXを使用してcontent.xmlからXMLコンテンツを解析します。
Test3.odt
content.xml
3764
1 My New Great Paper
2 Abstract
2 Introduction
2 Content
3 More content
3 Even more
2 Conclusions
サンプルコード:使用のContentHandlerの
public void printHeadingsOfOdtFIle(File odtFile) { try { ZipFile zFile = new ZipFile(odtFile); System.out.println(zFile.getName()); ZipEntry contentFile = zFile.getEntry("content.xml"); System.out.println(contentFile.getName()); System.out.println(contentFile.getSize()); XMLReader xr = XMLReaderFactory.createXMLReader(); OdtDocumentContentHandler handler = new OdtDocumentContentHandler(); xr.setContentHandler(handler); xr.parse(new InputSource(zFile.getInputStream(contentFile))); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new OdtDocumentStructureExtractor().printHeadingsOfOdtFIle(new File("Test3.odt")); }
関連部品のようになります。
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
temp = "";
if("text:h".equals(qName)) {
String headingLevel = atts.getValue("text:outline-level");
if(headingLevel != null) {
System.out.print(headingLevel + " ");
}
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
char[] subArray = new char[length];
System.arraycopy(ch, start, subArray, 0, length);
temp = new String(subArray);
fullText.append(temp);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if("text:h".equals(qName)) {
System.out.println(temp);
}
}
サンプルコードは、単純にすべての見出しを出力します