User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,580 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,582 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 528 | Replies: 5
Reply
Join Date: Nov 2007
Posts: 4
Reputation: gvi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
gvi gvi is offline Offline
Newbie Poster

XML parsing issues - pls.help

  #1  
Nov 3rd, 2007
Hi Everyone,
I have the following xml from which I need a particular output .
  1.  
  2. <items>
  3. <item id="film">
  4. <category>entertainment </category>
  5. <category>drama</category>
  6. <category>music </category>
  7. </item>
  8. <item id="sitcom">
  9. <category>entertainment </category>
  10. <category>tv</category>
  11. </item>
  12.  
  13. </items>

Output I would like

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

  1. static void GetItem(Document doc){
  2. Element root = doc.getDocumentElement();
  3. Element[] items = getElementsByTagNameNR(root,"Item");
  4. for(int i=0;i<items.length;i++){
  5. String itemIDStr = items[i].getAttribute("ItemID");
  6. NodeList nl = doc.getElementsByTagName("Category");
  7. for (int j = 0; j < nl.getLength(); j++){
  8. Node n = nl.item(j);
  9. NodeList nll = n.getChildNodes();
  10. for(int k=0; k<nll.getLength(); k++){
  11. Node nn = nll.item(k);
  12. streamItemCategory.println(itemIDStr + nn.getNodeValue() + columnSeparator);
  13. }
  14.  
  15. }
  16. }
  17.  

I have include a condition by which only the categories for that item should be displayed and when the itemid changes it no more should include the old categories


Can someone tell me how to do it.

Thanks
G
Last edited by gvi : Nov 3rd, 2007 at 1:09 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: XML parsing issues - pls.help

  #2  
Nov 3rd, 2007
NodeList nl = doc.getElementsByTagName("Category");
That's where you go wrong.
Instead of retrieving the nodes "Category" from the document root, get them from each "Item" node in turn (and handle them before continuing to the next "Item" node of course).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Nov 2007
Posts: 4
Reputation: gvi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
gvi gvi is offline Offline
Newbie Poster

Re: XML parsing issues - pls.help

  #3  
Nov 3rd, 2007
Originally Posted by jwenting View Post
NodeList nl = doc.getElementsByTagName("Category");
That's where you go wrong.
Instead of retrieving the nodes "Category" from the document root, get them from each "Item" node in turn (and handle them before continuing to the next "Item" node of course).


Thanks for the response. But the problem is there is not a fixed # of category for an item. For one there can be 3 and for a different item there can be 2 category. And I need all the categories delimited by a special character like this

item1 category1
item1 category2
item1 category3

item2 category1
item2 category2

I am sorry I have misunderstood ur solution. I am just wondering how do I know how may category strings to declare if my code does something like this
 String CategoryStr = items[i].getElementTextByTagNameNR(items[i],"Category");
 

Pls.let me know if my reqmnt is not clear

Thanks
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: XML parsing issues - pls.help

  #4  
Nov 3rd, 2007
First you get a NodeList containing all "Item" elements:
NodeList items = doc.getElementsByTagName("Item");
You iterate over that retrieving the NodeList of "Category" elements for each:
NodeList categories = item.getElementsByTagName("Category");

You'll have to cast the Node instances you get from the NodeList to Element to do this, but that's alright, they're really Elements anyway.
Element item = (Element) items.item(i);
NodeList has a getLength() method to find out how many items it contains.

There's no need for the getElementsByTagNameNR method you (I suppose) wrote at all, the standard DOM functionality will get you everything you want.
You don't declare any specific number of Strings anywhere, just create a Collection of them.
If you really want to store everything for later printing (instead of printing stuff as you find it), use a Map<String, Collection<String>> containing the Item id as keys and Collections of Categories as values.
That way you can handle any size data at any time.

There's no need for arrays here at all, using them only confuses things (it often does, unless you're talking fixed length data of a nature that dictates it, in which case it's often known at compile time or program startup how large the array is going to get).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Nov 2007
Posts: 4
Reputation: gvi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
gvi gvi is offline Offline
Newbie Poster

Re: XML parsing issues - pls.help

  #5  
Nov 5th, 2007
Thanks Jwenting. I implementd ur idea and though intially i strugled but finally it worked

Thanks for your time
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: XML parsing issues - pls.help

  #6  
Nov 6th, 2007
good. The XML APIs take some getting used to, but they're quite powerful once you get to know them.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 6:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC