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);
}
}
샘플 코드는 단순히 모든 제목을 출력