tdf / odftoolkit

Java ODF toolkit project
https://odftoolkit.org/
Apache License 2.0
115 stars 45 forks source link

Add FAQ - How to access a specific ODT file and to do the required manipulation. #93

Open Muhammedismail opened 3 years ago

Muhammedismail commented 3 years ago

I have loaded an ODT file and trying to access the specific elements for the desired manipulation (editing and adding some data content)

But I am unable to get that

this is how I am trying to access the specific element: but I do have an issue with the OdfNamespace(), can anyone please give me any guide or a sample code for how to access a specific element to do the manipulation, if not this way or any other way please, basically I need to access the table content.

and I got the below from the Odf document.

OdfPackage pkg = OdfPackage.loadPackage(documentPath);
    OdfTextDocument odt = (OdfTextDocument) OdfDocument.loadDocument(documentPath);

      XPath xpath = XPathFactory.newInstance().newXPath();
      xpath.setNamespaceContext(new  OdfNamespace());
      OdfFileDom dom = odt.getContentDom();
      firstParagraph = (TextPElement) xpath.evaluate("//text:p[1]", dom, XPathConstants.NODE);

it would be really great if anybody can give me any hint or guidance, thanks.

svanteschubert commented 3 years ago

@Muhammedismail Hej, I take this as a documentation issue. I usually start to look in existing tests to copy/paste existing code and you might want to take a look at least into the following three:

  1. https://github.com/tdf/odftoolkit/blob/master/odfdom/src/test/java/org/odftoolkit/odfdom/dom/XPathTest.java
  2. https://github.com/tdf/odftoolkit/blob/master/odfdom/src/test/java/org/odftoolkit/odfdom/doc/text/OdfParagraphTest.java
  3. https://github.com/tdf/odftoolkit/blob/master/odfdom/src/test/java/org/odftoolkit/odfdom/doc/text/OdfSpanTest.java

An easy consistent high-level API is the next big thing for the 1.0.0