简体   繁体   中英

Attribute value is null while trying to parse XML using dom4j

I am trying to parse a XML string using dom4j. But when I try to get the attribute value of any node(element) it return null value only.

This is my 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 my 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();

            for (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 the above code:

------------------------------------------------
Provider--> null
EventID--> 63
Level--> 4
Task--> 0
Keywords--> 0x80000000000000
TimeCreated--> null
EventRecordID--> 27487
Channel--> Application
Computer--> eGLAP0011-PC
------------------------------------------------

In the above output, the value of Provider and Time Created is null, I have searched lot in websites, but I didn't get correct solution for this problem, Please share your ideas. Thanks in advance,

Your current node is <System> . The elementText() function searches that node, get its text and prints it, but attributeValue() refers to current node, so you have to advance to the target and call it there, like:

System.out.println("Provider--> " + Sys.element("Provider").attributeValue("Name"));

and

System.out.println("TimeCreated--> " + Sys.element("TimeCreated").attributeValue("SystemTime"));

So, replace it in your code and give it a try. It 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
------------------------------------------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM