Help with Java and XML

T

Tom

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
 
A

Andrew Thompson

If I find a <mediaobject>, getParent() only returns another Element.
But comments are no Elements, just Content.

I think that is the basic problem with your current approach.[1]

You may need to transform the document before processing,
or do a simpler search using String.indexOf to look for
your comments.

[1] I'll think some more and get back to this if I have any bright
ideas, but thought that should be highlighted/stated now.

[ Note: Follow-Ups to this post set to c.l.j.programmer only. ]

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"The captain seemed to understand, because the next day the cap' went out
and drafted a band."
The Andrews Sisters 'Boogie Woogie Bugle Boy'
 
T

Tjerk Wolterink

Tom said:
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


Use the getContent() method of the Element class like this:

void walk( Element node, Collection allNodes )
{
// Add this node
allNodes.add( node );
// Recurse to add children
Iterator i = node.getContent().iterator();
while( i.hasNext() ) {
org.jdom.Content content=(org.jdom.Content)i.next();
if(content instanceof Comment) {
// ok we have a comment
Comment c=(Comment)content;
}
if(content instanceof Element) {
walk( (Element) content, allNodes );
}
}
}


Hope this helps you,

please look at the jdom api for more help.
I got this answer from the api...
so learn how to use a api.
 
R

Raymond DeCampo

Tom said:
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.

I haven't used JDOM, so this answer may confuse you because for some
reason JDOM duplicates much of the DOM without actually being the DOM.
(JDOM was written by smart people AFAIK, so there is probably a good
reason.) So my comments apply to the standard DOM, i.e. the package
org.w3c.dom which is bundled with the JDK.

Everything in the XML document is a Node, even a comment. When you have
a <mediaobject> node, you can use Node.getPreviousSibling() to next the
node preceding it. If this Node is not a comment type (see
Node.getNodeType()), then you need to add the comment.

Note that if the document is not normalized (see Document.normalize()),
you may get spurious text nodes between the comment and <mediaobject>
element.

HTH,
Ray
 
A

Andrew Thompson

Everything in the XML document is a Node, even a comment.

Huh, interesting! That doesn't gel with my (more limited)
experience. I had not realised that any Java parsing method
represented comments in the final, parsed structure.

[ Follow-Ups to this post set to comp.lang.java.programmer only. ]
 
T

Tom

I think the code from Tjerk Wolterink gave me a hint in the right
direction.
Thank you all for your suggestions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top