Results 1 to 7 of 7
- 11-02-2007, 06:49 AM #1
Member
- Join Date
- Oct 2007
- Posts
- 6
- Rep Power
- 0
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
when I traverse through the elementsJava 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>
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:
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.Java 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); }
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
GLast edited by JavaBean; 11-02-2007 at 09:34 AM.
- 11-02-2007, 08:44 AM #2
Member
- Join Date
- Nov 2007
- Location
- BANGALORE,INDIA
- Posts
- 3
- Rep Power
- 0
try the following code:
others please optimise the code.Java 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()); } }Last edited by JavaBean; 11-02-2007 at 09:33 AM.
- 11-02-2007, 09:33 AM #3
Hello friends,
I placed your codes inside [code] tag. Please use [code] tag for your codes in your further posts.
- 11-02-2007, 10:48 PM #4
Member
- Join Date
- Oct 2007
- Posts
- 6
- Rep Power
- 0
Thanks Murali for your time and code
That did work.
- 11-03-2007, 12:50 AM #5
Member
- Join Date
- Oct 2007
- Posts
- 6
- Rep Power
- 0
Murali I have another question
from the xml below I need the following output
film|entertainmentJava 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|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 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 conditionJava 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); } } }
As again your help needed
Thanks
GLast edited by JavaBean; 11-03-2007 at 07:37 AM.
- 11-03-2007, 07:35 AM #6
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.
- 11-08-2007, 05:48 PM #7
Member
- Join Date
- Nov 2007
- Location
- Singapore
- Posts
- 3
- Rep Power
- 0
Hi gvi,
try this code:
Regards,Java 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() ); } } } }
Putrama
Similar Threads
-
Reading an already opened file
By Java Tip in forum Java TipReplies: 0Last Post: 01-25-2008, 06:50 PM -
Reading a file
By mew in forum New To JavaReplies: 2Last Post: 12-30-2007, 12:23 PM -
Reading a file for use
By peachyco in forum New To JavaReplies: 2Last Post: 11-27-2007, 03:49 AM -
Help with File reading and writing
By baltimore in forum New To JavaReplies: 1Last Post: 07-31-2007, 06:47 PM -
Reading from a file
By leebee in forum New To JavaReplies: 1Last Post: 07-23-2007, 12:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks