java - Attribute value is null while trying to parse XML using dom4j -
i trying parse xml string using dom4j. when try attribute value of node(element) return null value only.
this file contains xml:
<event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'> <system> <provider name='outlook'/> <eventid qualifiers='16384'>63</eventid> <level>4</level> <task>0</task> <keywords>0x80000000000000</keywords> <timecreated systemtime='2015-07-23t13:45:26.000000000z'/> <eventrecordid>27487</eventrecordid> <channel>application</channel> <computer>eglap0011-pc</computer> </system> <eventdata> <data>the exchange web service request getappmanifests succeeded. </data> </eventdata> </event>
see below code:
bufferedreader br = null; try { file inputfile = new file("d:\\eventlog\\xml_op.txt"); br = new bufferedreader(new filereader(inputfile)); string line="",con_line=""; int inc =0; while((line = br.readline())!=null) { con_line+=line; } document doc=documenthelper.parsetext(con_line); element parent_ele = doc.getrootelement(); (iterator i1 = parent_ele.elementiterator("system"); i1.hasnext();) { element sys = (element) i1.next(); system.out.println("------------------------------------------------"); system.out.println("provider--> " + sys.attributevalue("name")); //i got null value here system.out.println("eventid--> " + sys.elementtext("eventid")); system.out.println("level--> " + sys.elementtext("level")); system.out.println("task--> " + sys.elementtext("task")); system.out.println("keywords--> " + sys.elementtext("keywords")); system.out.println("timecreated--> " + sys.attributevalue("systemtime"));//i got null value here system.out.println("eventrecordid--> " + sys.elementtext("eventrecordid")); system.out.println("channel--> " + sys.elementtext("channel")); system.out.println("computer--> " + sys.elementtext("computer")); system.out.println("------------------------------------------------"); } } catch(exception e) { e.printstacktrace(); }
the output of above code:
------------------------------------------------ provider--> null eventid--> 63 level--> 4 task--> 0 keywords--> 0x80000000000000 timecreated--> null eventrecordid--> 27487 channel--> application computer--> eglap0011-pc ------------------------------------------------
in above output, value of provider , time created null, have searched lot in websites, didn't correct solution problem, please share ideas. in advance,
your current node <system>
. elementtext()
function searches node, text , prints it, attributevalue()
refers current node, have advance target , call there, like:
system.out.println("provider--> " + sys.element("provider").attributevalue("name"));
and
system.out.println("timecreated--> " + sys.element("timecreated").attributevalue("systemtime"));
so, replace in code , give try. yields:
------------------------------------------------ provider--> outlook eventid--> 63 level--> 4 task--> 0 keywords--> 0x80000000000000 timecreated--> 2015-07-23t13:45:26.000000000z eventrecordid--> 27487 channel--> application computer--> eglap0011-pc ------------------------------------------------
Comments
Post a Comment