Link to home
Start Free TrialLog in
Avatar of akashsuresh
akashsureshFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Adding element values of an xml file to a linked list

I have an svn log file in xml,
<logentry
   revision="22">
<author>jenselstner</author>
<date>2010-06-14T06:38:13.426483Z</date>
<paths>
<path
   action="M"
   kind="file">/trunk/WCFCommunicationProvider/WCFCommunicationProvider.cs</path>
</paths>
<msg>
</msg>
</logentry>

Open in new window


Now I made a linked list to store each logentry as an object.I have had no problems with author,date and message.

LogProcess1(int rev, String auth, String date,
			List<List<PathInfo1>> pathinfolist)

Open in new window


Now as you can see I made a list of list PathInfo1

PathInfo1 is like this

PathInfo1(String action,String kind,String filepath)

Open in new window


Now this is my code

for(int t=0;t<logentrylist.getLength();t++){
					Node list1=logentrylist.item(t);
						
					if(list1.getNodeType()==Node.ELEMENT_NODE)
					{
						Element listEntryelement=(Element)list1;
						Element le=(Element)logentrylist.item(t);
						
						NamedNodeMap nodelogmap=le.getAttributes();
						String rev=((Node)nodelogmap.item(0)).getNodeValue().trim();
						revision=Integer.parseInt(rev);
						NodeList authlist=listEntryelement.getElementsByTagName("author");
						Element authelement=(Element)authlist.item(0);
						if(authelement!=null){
						NodeList authsublist=authelement.getChildNodes();
						author=((Node) authsublist.item(0)).getNodeValue().trim();
						
						}
						else{
							author="Anonymous";
						}
						
						NodeList datelist=listEntryelement.getElementsByTagName("date");
						Element dateelement=(Element)datelist.item(0);
						NodeList datesublist=dateelement.getChildNodes();
						date=((Node) datesublist.item(0)).getNodeValue().trim();
						
						NodeList pathentrylist=d1.getElementsByTagName("paths");
						for(int y=0;y<pathentrylist.getLength();y++){
							
							Node list2=pathentrylist.item(y);
							
							Element pathEntryelement=(Element)list2;
							
						NodeList pathlist=pathEntryelement.getElementsByTagName("path");
						
						
						
						for(int i=0;i<pathlist.getLength();i++)
						{//System.out.println(pathlist.getLength());
							Element pe=(Element)pathlist.item(i);
							
							NamedNodeMap peattr=pe.getAttributes();
							temp=peattr.item(0);
							temp1=peattr.item(1);
							
							path1=new PathInfo1(temp.getNodeValue(),temp1.getNodeValue(),pe.getTextContent());
							pathinf.add(path1);
							
							System.out.println(pathinf.get(i));
							
						}
						pathinfo.add(pathinf);
						
						
						
						//pathinf.clear();
						
						}
						
						
						LogProcess1 lp=new LogProcess1(revision,author,date,pathinfo);
						logobject.add(lp);
					}

Open in new window


And the declarations are

List<LogProcess1> logobject=new LinkedList<LogProcess1>();
List<List<PathInfo1>> pathinfo=new LinkedList<List<PathInfo1>>();
List<PathInfo1> pathinf=new LinkedList<PathInfo1>();

Open in new window



However my code seems to run more than expected and I dont get what I want.an someone help me out?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You will find this a LOT easier to do with Commons Digester. It will create your top level object and its nested List automatically if you give it a pattern and the bean type
Avatar of akashsuresh

ASKER

Unfortunately I am already in the last stage of this code,and changing does not seem feasible to me at this point of time.

Could you tell me where I am going wrong in the current code.
Well you've made it difficult to help you - because you've made it difficult for yourself ;)

You'll have to be quite precise about WHY it's wrong for you
Well,the first loop takes in 22 objects,since the number of revisions is 22.The second loop considers the paths tag,and the the third one considers the path tag,I was trying to get the paths of each into the list.

No the List<List<PathInfo1>> should be of size 22.But I am getting 484.
Ok, your first problem will be that you are using the same list over and over, and so it is just growing rather than having separate lists in each LogProcess object. To fix this insert the following line before line 28 of the above...

pathinfo=new LinkedList<List<PathInfo1>>();

Open in new window

and insert the following line before line 39 of the above...

pathinf=new LinkedList<PathInfo1>();

Open in new window


Now, once you have got it working, you can probably change quite a bit of the above by assuming that there would only ever be 1 <paths> element within each <logentry>, (there can still be multiple <path> elements in that 1 <paths> though). You could simplify the above so that you don't need the "list of lists", your declarations would just be...

List<LogProcess1> logobject=new LinkedList<LogProcess1>();
List<PathInfo1> pathinfo=new LinkedList<PathInfo1>();

Open in new window

What I'm saying in terms of the code above, is that pathentrylist.getLength() will only ever be max of 1 and so that outer loop (the one with 'y' as the loop index) is not necessary. But, yeah, get it working with the above fix first so that you at least have something!
I tried this but now all path elements are being stored into each object.
Can you post your updated code please?
package interfaces;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class LogFileParser {
	public static ArrayList<String> al;
	LogProcess lp4;
	public static List<LogProcess> fileparse(String fname)
	{int l=0;
	PathInfo path1=new PathInfo(null,null,null);
		List<LogProcess> logobject=new LinkedList<LogProcess>();
		List<PathInfo> pathinfo=new LinkedList<PathInfo>();
		List<PathInfo> alp4=new LinkedList<PathInfo>();
		ArrayList<String> pi10=new ArrayList<String>();
		
		try{
			
				DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
			
				DocumentBuilder db=dbf.newDocumentBuilder();
				Document d1=db.parse(fname);
				d1.getDocumentElement().normalize();
				
				NodeList logentrylist=d1.getElementsByTagName("logentry");
		
					Node n=logentrylist.item(0);
					Node att=n.getAttributes().getNamedItem("revision");
					
				NodeList list=d1.getElementsByTagName("author");
				
				int revision;
				Node temp,temp1;
				String author,date,msg;
				al=new ArrayList<String>();
				
				for(int t=0;t<logentrylist.getLength();t++){
					Node list1=logentrylist.item(t);
						
					if(list1.getNodeType()==Node.ELEMENT_NODE)
					{
						Element listEntryelement=(Element)list1;
						Element le=(Element)logentrylist.item(t);
						
						NamedNodeMap nodelogmap=le.getAttributes();
						String rev=((Node)nodelogmap.item(0)).getNodeValue().trim();
						revision=Integer.parseInt(rev);
						NodeList authlist=listEntryelement.getElementsByTagName("author");
						Element authelement=(Element)authlist.item(0);
						if(authelement!=null){
						NodeList authsublist=authelement.getChildNodes();
						author=((Node) authsublist.item(0)).getNodeValue().trim();
						
						}
						else{
							author="Anonymous";
						}
						
						NodeList datelist=listEntryelement.getElementsByTagName("date");
						Element dateelement=(Element)datelist.item(0);
						NodeList datesublist=dateelement.getChildNodes();
						date=((Node) datesublist.item(0)).getNodeValue().trim();
						
												
						NodeList pathlist=listEntryelement.getElementsByTagName("path");
						
						
						
						for(int i=0;i<pathlist.getLength();i++)
						{//System.out.println(pathlist.getLength());
							Element pe=(Element)pathlist.item(i);
							
							NamedNodeMap peattr=pe.getAttributes();
							temp=peattr.item(0);
							temp1=peattr.item(1);
							
							path1=new PathInfo(temp.getNodeValue(),temp1.getNodeValue(),pe.getTextContent());
							pathinfo.add(path1);
							
							
						}
						LogProcess lp=new LogProcess(revision,author,date,pathinfo);
						logobject.add(lp);
					}
						//System.out.println(pathinfo.size());	
						
						/*for(LogProcess lp3:logobject)
						{
							for(List<PathInfo> pi3:pathinfo)
							{
							  for(PathInfo pi7:pathinf)
							  {
								System.out.println(pi7.getFilepath());
							  }
							}
						}*/
				}
				
				//System.out.println(pi10.size());
				
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		return logobject;
		
	}
	
	
}

Open in new window

The issue is that for example,if I want to find the number of paths of a particular author,I cant do that,since every log entry now has 328 files.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect.Thank you
Not a problem, glad to help!