Results 1 to 7 of 7
  1. #1
    gvi
    gvi is offline Member
    Join Date
    Oct 2007
    Posts
    6
    Rep Power
    0

    Post 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

    Java 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:

    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);
    
    }
    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 09:34 AM.

  2. #2
    murali_java is offline Member
    Join Date
    Nov 2007
    Location
    BANGALORE,INDIA
    Posts
    3
    Rep Power
    0

    Default

    try the following 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());				
    			}
    		}
    others please optimise the code.
    Last edited by JavaBean; 11-02-2007 at 09:33 AM.

  3. #3
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Hello friends,

    I placed your codes inside [code] tag. Please use [code] tag for your codes in your further posts.

  4. #4
    gvi
    gvi is offline Member
    Join Date
    Oct 2007
    Posts
    6
    Rep Power
    0

    Default

    Thanks Murali for your time and code

    That did work.

  5. #5
    gvi
    gvi is offline Member
    Join Date
    Oct 2007
    Posts
    6
    Rep Power
    0

    Default

    Murali I have another question

    from the xml below I need the following output

    Java 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

    Java 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 07:37 AM.

  6. #6
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    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.

  7. #7
    made.putrama is offline Member
    Join Date
    Nov 2007
    Location
    Singapore
    Posts
    3
    Rep Power
    0

    Default

    Hi gvi,

    try this code:
    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() );
            		}
            	}
            }
    }
    Regards,
    Putrama

Similar Threads

  1. Reading an already opened file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-25-2008, 06:50 PM
  2. Reading a file
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-30-2007, 12:23 PM
  3. Reading a file for use
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 03:49 AM
  4. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 PM
  5. Reading from a file
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 12:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •