Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-02-2007, 07:49 AM
gvi gvi is offline
Member
 
Join Date: Oct 2007
Posts: 6
gvi is on a distinguished road
Question abt.reading xml file using java
Hi Everyone
I am not an advanced user and pardon me if this is not an advanced topic for you.

My requirement is this. I have a xml file from which I have to extract data part alone and write to a data file according to the database tables(relations) I have created

I have the following xml

Code:
<items> <item id="film"> <category>entertainment </category> <category>drama</category> <category>music </category> </item> <item id="sitcom"> <category>entertainment </category> <category>tv</category> </item> </items>
when I traverse through the elements
I am able to extract entertainment
entertainment

but not
entertainment
drama
music
entertainment
tv
I hope you understand my issue. Its just that I want all the categories (it would be great to eliminate duplicate values).

This is the code I wrote:

Code:
for (int i =0; i< items.length; i++) { String catStr = getElementTextByTagNameNR(items[i], "Category"); String catStr1 = getElementTextByTagNameNR(items[i], "Category"); String catStr2 = getElementTextByTagNameNR(items[i], "Category"); String catStr3 = getElementTextByTagNameNR(items[i], "Category"); System.out.println(catStr + catStr1+ catStr2 + catStr3 + columnSeparator); }
My question is how would I make sure it goes to the next Category and not read the first category. I would not be able to make the category name different.

This is the DTD for the xml document

<!Element item(Name,Category+)>

So I would like to get all categories an item belong to and not the first one alone

Thanks
G

Last edited by JavaBean : 11-02-2007 at 10:34 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-02-2007, 09:44 AM
Member
 
Join Date: Nov 2007
Location: BANGALORE,INDIA
Posts: 3
murali_java is on a distinguished road
try the following code:

Code:
Document doc = db.parse (new File("c:\\exp\\item.xml")); NodeList nl =doc.getElementsByTagName("category"); System.out.println(" no of nodes for root = "+ nl.getLength()); for(int i =0 ;i<nl.getLength();i++){ Node n = nl.item(i); System.out.print(n.getNodeName()); NodeList nll=n.getChildNodes(); for(int ii =0 ;ii<nll.getLength();ii++){ Node nn = nll.item(ii); System.out.println("==>"+nn.getNodeValue()); } }
others please optimise the code.

Last edited by JavaBean : 11-02-2007 at 10:33 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-02-2007, 10:33 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Hello friends,

I placed your codes inside [code] tag. Please use [code] tag for your codes in your further posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-02-2007, 11:48 PM
gvi gvi is offline
Member
 
Join Date: Oct 2007
Posts: 6
gvi is on a distinguished road
Thanks Murali for your time and code

That did work.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-03-2007, 01:50 AM
gvi gvi is offline
Member
 
Join Date: Oct 2007
Posts: 6
gvi is on a distinguished road
Murali I have another question

from the xml below I need the following output

Code:
<items> <item id="film"> <category>entertainment </category> <category>drama</category> <category>music </category> </item> <item id="sitcom"> <category>entertainment </category> <category>tv</category> </item> </items>
film|entertainment
film|drama
film|music
sitcom|entertainment
sitcom|tv

But the code I have displays the output as

film|entertainment
film|drama
film|music
film|entertainment
film|tv

sitcom|entertainment
sitcom|drama
sitcom|music
sitcom|entertainment
sitcom|tv

And this is the code I have

Code:
static void GetItem(Document doc){ Element root = doc.getDocumentElement(); Element[] items = getElementsByTagNameNR(root,"Item"); for(int i=0;i<items.length;i++){ String itemIDStr = items[i].getAttribute("ItemID"); NodeList nl = doc.getElementsByTagName("Category"); for (int j = 0; j < nl.getLength(); j++){ Node n = nl.item(j); NodeList nll = n.getChildNodes(); for(int k=0; k<nll.getLength(); k++){ Node nn = nll.item(k); streamItemCategory.println(itemIDStr + nn.getNodeValue() + columnSeparator); } } }
I know I have to check somewhere where when the itemId changes it no more should read the previous ones categories but not sure where to include the condition

As again your help needed

Thanks
G

Last edited by JavaBean : 11-03-2007 at 08:37 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-03-2007, 08:35 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
gvi, when you use [code] tag, you start it with [ code ] (without spaces) and finish it with [ /code ] (without spaces) tag just like in html except that we are using square brackets around the tag here. I am fixing your post now.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-08-2007, 06:48 PM
Member
 
Join Date: Nov 2007
Location: Singapore
Posts: 3
made.putrama is on a distinguished road
Hi gvi,

try this code:
Code:
static void GetItem(Document doc){ Element root = doc.getDocumentElement(); NodeList items = root.getElementsByTagName("item"); for(int i=0;i<items.getLength();i++){ Element item = (Element)items.item(i); String id = item.getAttribute("id"); NodeList categs = item.getChildNodes(); for(int j=0;j<categs.getLength();j++){ Node categ = categs.item(j); if( categ instanceof Element){ Element el = (Element)categ; Text t = (Text)el.getFirstChild(); System.out.println(id+"|"+ t.getNodeValue() ); } } } }
Regards,
Putrama
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading an already opened file Java Tip Java Tips 0 01-25-2008 07:50 PM
Reading a file mew New To Java 2 12-30-2007 01:23 PM
Reading a file for use peachyco New To Java 2 11-27-2007 04:49 AM
Help with File reading and writing baltimore New To Java 1 07-31-2007 07:47 PM
Reading from a file leebee New To Java 1 07-23-2007 01:02 PM


All times are GMT +3. The time now is 03:26 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org