【问题标题】:Attribute value is null while trying to parse XML using dom4j尝试使用 dom4j 解析 XML 时属性值为 null
【发布时间】:2015-07-27 13:09:30
【问题描述】:

我正在尝试使用 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, 我在网站上搜索了很多,但我没有得到正确的解决方案,请分享您的想法。提前致谢,

【问题讨论】:

  • 你不会直接从文件中得到TimeCreated

标签: java xml parsing xml-parsing


【解决方案1】:

您当前的节点是&lt;System&gt;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
------------------------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    相关资源
    最近更新 更多