•
•
•
•
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
![]() |
•
•
Join Date: Nov 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Hi Everyone,
I have the following xml from which I need a particular output .
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
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
I have the following xml from which I need a particular output .
xml Syntax (Toggle Plain Text)
<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>
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
java Syntax (Toggle Plain Text)
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 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.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 19
Solved Threads: 200
NodeList nl = doc.getElementsByTagName("Category");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.
•
•
Join Date: Nov 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
That's where you go wrong.NodeList nl = doc.getElementsByTagName("Category");
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
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 19
Solved Threads: 200
First you get a NodeList containing all "Item" elements:
You iterate over that retrieving the NodeList of "Category" elements for each:
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.
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).
NodeList items = doc.getElementsByTagName("Item");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);
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Need a PHP programmer with XML experience. (Web Development Job Offers)
- Read XML File in C/C++ (C++)
- Xml Parsing using Sax (Java)
- xml parsing from java (Java)
- Searching XML documents with PHP (PHP)
- how xml is useful? (RSS, Web Services and SOAP)
- Java and XML (Java)
Other Threads in the Java Forum
- Previous Thread: Dijkstra Help
- Next Thread: Interface ... extends ... implements ... (Need help)



Linear Mode