尝试使用dom4j解析XML时,属性值为null

时间:2015-07-27 13:09:31

标签: java xml parsing xml-parsing

我正在尝试使用dom4j解析XML字符串。但是当我尝试获取任何节点(元素)的属性值时,它只返回空值。

这是我的文件包含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>

见下面我的代码:

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();
        }

以上代码的输出:

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

在上面的输出中,Provider和Time Created的值为null, 我在网站上搜索过很多,但我没有找到解决这个问题的正确方法,请分享您的想法。提前谢谢,

1 个答案:

答案 0 :(得分:1)

您当前的节点是<System>elementText()函数搜索该节点,获取其文本并打印它,但attributeValue()引用当前节点,因此您必须前进到目标并在那里调用它,如:

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

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

所以,在代码中替换它并尝试一下。它产生:

------------------------------------------------
Provider--> Outlook
EventID--> 63
Level--> 4
Task--> 0
Keywords--> 0x80000000000000
TimeCreated--> 2015-07-23T13:45:26.000000000Z
EventRecordID--> 27487
Channel--> Application
Computer--> eGLAP0011-PC
------------------------------------------------