Hi Everyone,
I have the following xml from which I need a particular output .

<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

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

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).

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

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).

Thanks Jwenting. I implementd ur idea and though intially i strugled but finally it worked

Thanks for your time

good. The XML APIs take some getting used to, but they're quite powerful once you get to know them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.