Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Help with Java and XML

Reply
Thread Tools

Help with Java and XML

 
 
Tom
Guest
Posts: n/a
 
      09-08-2005
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

 
Reply With Quote
 
 
 
 
Sameer
Guest
Posts: n/a
 
      09-08-2005
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/

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      09-08-2005
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'
 
Reply With Quote
 
Tjerk Wolterink
Guest
Posts: n/a
 
      09-08-2005
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.
 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      09-08-2005
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.
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-09-2005
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'
 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      09-09-2005
Ok, that is a good approach. I will work with that. Tank you.

 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      09-09-2005
I think the code from Tjerk Wolterink gave me a hint in the right
direction.
Thank you all for your suggestions.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xml to java object to xml - is xmlbeans ok? - help reqd ananth Java 2 11-08-2006 02:28 PM
[Announce] Stylus Studio 2007 XML Enterprise Suite Released: XML Pipeline, XML Publishing and Data Conversion API's stylusstudio@gmail.com Java 0 09-26-2006 05:04 PM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
What XML technologies to learn first for "XML Processing" and "XML Mapping"? Bomb Diggy Java 0 07-28-2004 07:26 AM
Help on including one XML document within another XML document using XML Schemas Tony Prichard XML 0 12-12-2003 03:18 PM



Advertisments