473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with Java and XML

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

Sep 8 '05 #1
7 1381
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/

Sep 8 '05 #2
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'
Sep 8 '05 #3
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.
Sep 8 '05 #4
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.
Sep 8 '05 #5
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'
Sep 9 '05 #6
Tom
Ok, that is a good approach. I will work with that. Tank you.

Sep 9 '05 #7
Tom
I think the code from Tjerk Wolterink gave me a hint in the right
direction.
Thank you all for your suggestions.

Sep 9 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jagdeesh | last post by:
Hai Colleagues, I am using Tomcat 4.1.24 and JDK 1.4.0_03 in my winXP machine. I've transferred a set of folders(containing jsp files) into tomcat's webapps directory(to /webapps/bob ,...
4
by: Allan Robertson | last post by:
Hello I've downloaded the Java SDK 1.4.0 from Sun's website in the hope to learning Java, however when I compile my *.java files this (at most times) works fine, however when I run the *.class...
0
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
0
by: Shawn | last post by:
I am getting the following error with a Java Applet being served out by IIS over HTTPS/SSL using a Verisign certificate: java.lang.NoClassDefFoundError: javax/help/HelpSetException at...
5
by: Grant Collins | last post by:
Hi I am writing a web based application as part of a small project that I am undertaking using servlets beans and jsp. I already have one servlet - bean - jsp page working and I have written...
1
by: glenn123 | last post by:
Hi, i am just about out of time to produce a working jukebox which has to perform these functions: to play music files when a track is chosen from a list which when the user presses the change genre...
0
by: yksoni | last post by:
we are working in IBM aglets 2.0.2 with mysql 4.0.2. When we try to access database through tahiti server i.e.aglets we get following error. java.net.SocketException MESSAGE:...
0
by: wb5plj | last post by:
Hi I am having a problem passing some sql to the db2cmd via ant. This is very confusing as I am doing this exact thing elseware with no problem (just differant sql, and I have verified the sql is...
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
2
by: btcoder | last post by:
Hi, my jsp page uses sun.net.smtp.SmtpClient to send email. It worked fine until the hosted location was moved to another server. Now it generates the sun.net.smtp.SmtpProtocolException and the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.