I need help to implement the following task in Java and any XML API but
preferably JDOM. I am a total newbie to this.
I have a DocBook document, for example:
<chapter>
<title>Title</title>
<para>
Text
</para>
<mediaobject>
<imageobject>
<imagedata fileref="image1.gif"/>
</imageobject>
</mediaobject>
<sect1>
<para>
Text
</para>
<mediaobject>
<imageobject>
<imagedata fileref="image2.gif"/>
</imageobject>
</mediaobject>
</sect1>
</chapter>
I want to find any <mediaobject> and prepend a comment containing the
imagename, like
<!-- image2 -->
<mediaobject>
<imageobject>
<imagedata fileref="image2.gif"/>
</imageobject>
</mediaobject>
The comment must not be prepended if it already exists! I have a
problem to check if there is a comment before the <mediaobject>. I
found a code to walk through all elements of the document with
void walk( Element node, Collection allNodes )
{
// Add this node
allNodes.add( node );
// Recurse to add children
Iterator i = node.getChildren().iterator();
while( i.hasNext() )
walk( (Element) i.next(), allNodes );
}
If I find a <mediaobject>, getParent() only returns another Element.
But comments are no Elements, just Content.
I don' know a way to search for all <mediaobjects> on a Content level.
Maybe some other approach will be better but I have absolutly no clue.
__
Tom