akashsuresh
asked on
Another piece of code on DOM seems to be giving a NullPointer Exception on <msg> tag
<logentry
revision="1">
<date>2000-07-13T10:28:31.000000Z</date>
<paths>
<path
action="A"
kind="">/tags</path>
<path
action="A"
kind="">/trunk</path>
<path
action="A"
kind="">/branches</path>
</paths>
<msg>New repository initialized by cvs2svn.</msg>
</logentry>
For the above the snippet of code to parse is:
NodeList msglist=listEntryelement.getElementsByTagName("msg");
Element msgelement=(Element)msglist.item(0);
if(msgelement!=null){
NodeList msgsublist=msgelement.getChildNodes();
msg=((Node) msgsublist.item(0)).getNodeValue().trim();
}
else
{
msg="No Message";
}
I am getting the exception at the line
msg=((Node) msgsublist.item(0)).getNod
ASKER
Well for every log entry there is one message,So for 100 log entries,100 messages.
Can you explain your last statement.I am not quite proficient in DOM.
This gave me an error.
Can you explain your last statement.I am not quite proficient in DOM.
NodeList msglist=listEntryelement.getElementsByTagName("msg");
Element msgelement=(Element)msglist.item(0);
if(msgelement!=null){
NodeList msgsublist=msgelement.getChildNodes();
Element text=(Element) msgsublist.item(0);
if(text!=null)
{
msg=((Node) text).getNodeValue().trim();
}
}
else
{
msg="No Message";
}
This gave me an error.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
NodeList msglist=listEntryelement.getElementsByTagName("msg");
Element msgelement=(Element)msglist.item(0);
if(msgelement.hasChildNodes()){
NodeList msgsublist=msgelement.getChildNodes();
msg=((Node) msgsublist.item(0)).getFirstChild().getNodeValue().trim();
}
else
{
msg="No Message";
}
Is this how you meant it to be?
It really depends on what kind of Node you're starting with. Using these DOM operation is hard work and highly error prone. You're doing yourself no favours by not using XPath. You could get all message text with
in one go
http://technojeeves.com/joomla/index.php/free/63-xpath-tester
//msg/text()
in one go
http://technojeeves.com/joomla/index.php/free/63-xpath-tester
ASKER
I understand that.However I have been working on this for a long time now and this is the last and final flaw to correct.I should have taken xpath from the very beginning but unfortunately I am already on the end of the road.
I would really appreciate if you could help me sort this out.
If you notice in the above code "datetist" works fine the same way,but msglist seems to mess up
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 = "None";
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");
pathinfo=new LinkedList<PathInfo>();
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);
}
NodeList msglist=listEntryelement.getElementsByTagName("msg");
Element msgelement=(Element)msglist.item(0);
if(msgelement.hasChildNodes()){
NodeList msgsublist=msgelement.getChildNodes();
msg=((Node) msgsublist.item(0)).getFirstChild().getNodeValue().trim();
}
else
{
msg="No Message";
}
LogProcess lp=new LogProcess(revision,author,date,pathinfo,msg);
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());
}
}
}*
I would really appreciate if you could help me sort this out.
If you notice in the above code "datetist" works fine the same way,but msglist seems to mess up
You'd be better off just getting the <msg> element directly off each <logentry>
ASKER
I tried that already.does not work.
Back later
ASKER
I found that the exception comes at the places where the tag shows <msg></msg>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You are a life saver.It works for me as well.Thank you so much
:)
How large is your log file btw?
How large is your log file btw?
Open in new window
I suspect there's only one text node though? If so, you might as well use hasChildNodes followed by getFirstChild