Page 1 of 1

How to delete all elements of a certain type

PostPosted: Tue Apr 17, 2012 12:47 am
by Nanocom
Hi,

I'm trying to delete all CTMarkup of a .docx file.
I already managed to rename all CTBookmarks of a docx with a visitor using TraversalUtil, and I was wondering if I could write a visitor that would delete elements (instead of modifying them).
How would you do that?
My first idea was to write this visitor, but I'm a bit stuck on how to do it the clean way :

Code: Select all
public final class DeleteCTMarkupsVisitor extends TraversalUtilVisitor<CTMarkup> {

  public DeleteCTMarkupsVisitor() {
    super();
  }

  @Override
  public void apply(CTMarkup markup) {
    parent = markup.getParent(); // => Would give me the direct parent... Is that clean ?
    // Then I would iterate over the parent's elements, find the markup and delete it...
  }

}


Thanks!

Re: How to delete all elements of a certain type

PostPosted: Tue Apr 17, 2012 8:22 pm
by Nanocom
I implemented my own postApply method, it worked :)

EDIT: it didn't work, see next post

Re: How to delete all elements of a certain type

PostPosted: Wed Apr 18, 2012 2:20 am
by Nanocom
Actually, it's harder than I thought...
I didn't manage to delete the CTMarkup.
My code :
Code: Select all
// I've got a list of CTMarkup and its parent, built thanks to my visitor (I add the CTMarkup and its parent during the apply)
for (final Pair<Object, CTMarkup> markup : _listToSuppress) {
  final Object locParent = locMarkup.getKey();

  if (parent instanceof ContentAccessor) { // Useless but secure
    final List<Object> childs = ((ContentAccessor)parent).getContent();
    childs.remove(markup.getValue().getParent()); // The child is well removed
  }
}


So, the child is removed from the list of childs of the parent, but when I save the document, the CTMarkup are still threre.
childs.remove doesn't work : I guess it's not an "alive" object as it is with getJAXBNodesViaXPath().
Any idea on how to fix this?