Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   Help with Java and XML (http://www.velocityreviews.com/forums/t169829-help-with-java-and-xml.html)

Tom 09-08-2005 07:57 AM

Help with Java and XML
 
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


Sameer 09-08-2005 08:03 AM

Re: Help with Java and XML
 
I am also a newbie to XML but I found the tutorials on these sites
quite helpful. Do try some.

http://www.developerlife.com/

http://www.w3schools.com/


Andrew Thompson 09-08-2005 08:17 AM

Re: Help with Java and XML
 
On 8 Sep 2005 00:57:25 -0700, Tom wrote:

> 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'

Tjerk Wolterink 09-08-2005 12:36 PM

Re: Help with Java and XML
 
Tom wrote:
> 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.

Raymond DeCampo 09-08-2005 05:25 PM

Re: Help with Java and XML
 
Tom wrote:
> 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

--
XML is the programmer's duct tape.

Andrew Thompson 09-09-2005 01:27 AM

Re: Help with Java and XML
 
On Thu, 08 Sep 2005 17:25:21 GMT, Raymond DeCampo wrote:

> 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. ]

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"Give me a ticket for an aeroplane, ain't got time to take a fast train.."
The Box Tops 'The Letter'

Tom 09-09-2005 11:38 AM

Re: Help with Java and XML
 
Ok, that is a good approach. I will work with that. Tank you.


Tom 09-09-2005 11:40 AM

Re: Help with Java and XML
 
I think the code from Tjerk Wolterink gave me a hint in the right
direction.
Thank you all for your suggestions.



All times are GMT. The time now is 04:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.