473,803 Members | 3,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getChildre n().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 1427
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.programme r 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.getChildre n().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.Conten t content=(org.jd om.Content)i.ne xt();
if(content instanceof Comment) {
// ok we have a comment
Comment c=(Comment)cont ent;
}
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.getChildre n().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.getPreviou sSibling() to next the
node preceding it. If this Node is not a comment type (see
Node.getNodeTyp e()), then you need to add the comment.

Note that if the document is not normalized (see Document.normal ize()),
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
3687
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 , /webapps/sue) and i have added the folders bob, sue in my server.xml(in the context path). When i am trying to run jsp files from my browser, it works fine. But, the following jsp files reports some exceptions which is quite hard to understand. Here is...
4
2420
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 file using java.exe I get this error message. D:\jdk1.3\bin>java FirstLine.class Exception in thread "main" java.lang.NoClassDefFoundError: FirstLine/class
0
9876
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 ********************************** package Celcom.Client;
0
4507
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 java.lang.Class.getDeclaredConstructors0(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Class.java:1590) at java.lang.Class.getConstructor0(Class.java:1762) at java.lang.Class.newInstance0(Class.java:276) at...
5
28007
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 another servlet - bean - jsp but I am hitting an error from tomcat that I have no idea why it is failing. I am getting an org.apache.jasper.JasperException: with the root cause being java.lang.ClassNotFoundException:
1
2294
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 button the list is populated with a list of that genre. I have got the interface done to satisfaction, my problem is that when i press the change genre button nothing happens and when i select a track to play from the list which is setvisible and...
0
1230
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: java.security.AccessControlException: access denied (java.net.SocketPer mission 127.0.0.1:3306 connect,resolve) STACKTRACE: java.net.SocketException: java.security.AccessControlException: access denied (j
0
4015
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 valid) but in this one I am having problems. Any time I run this I get the folowing message from the db2 clp window that opens up. SQL0104N An unexpected token "SET" was found following "<identifier>". Expected tokens may include: "NEW". ...
5
2159
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 quiz i have to do a password and a login. I managed to do the password but when i tried to join this to the whole program its not working. This is the main program: import java.util.*; import java.io.*; import java.util.Scanner; import...
2
3837
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 hosting company can't find what's wrong with it so I'm here hoping somebody can help. The hosting company tried sending email from the server and it was okay and found no restrictions that prevents my page to access the smtp server. What could cause...
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.