gpt4 book ai didi

java - 使用 dom4j 解析 xml 文件时无法获取值

转载 作者:太空宇宙 更新时间:2023-11-04 13:36:00 24 4
gpt4 key购买 nike

我正在尝试使用 dom4j 解析 xml,我尝试了一些逻辑,但无法从文件中获取完整的数据。

请参阅下面我的 XML 文件:

<Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'>
<System><Provider Name='Application Error'/>
<EventID Qualifiers='0'>1000</EventID>
<Level>2</Level>
<Task>100</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime='2015-07-29T04:33:30.000000000Z'/>
<EventRecordID>264</EventRecordID>
<Channel>Application</Channel>
<Computer>WIN-R8CUJUTBUMV</Computer>
<Security/>
</System>
<EventData>
<Data>AppCrasher.exe</Data>
<Data>0.0.0.0</Data>
<Data>55b239c6</Data>
<Data>AppCrasher.exe</Data>
<Data>0.0.0.0</Data>
<Data>55b239c6</Data>
<Data>c0000005</Data>
<Data>000113e8</Data>
<Data>308</Data>
<Data>01d0c9b7b7373caa</Data>
<Data>C:\eGurkha\CrashLogTest_Script\AppCrasher x64\AppCrasher.exe</Data>
<Data>C:\eGurkha\CrashLogTest_Script\AppCrasher x64\AppCrasher.exe</Data>
<Data>f6135af5-35aa-11e5-b2fe-005056b27da4</Data>
</EventData>
</Event>

这是我的java代码:

BufferedReader br = null;
try
{
File inputFile = new File("D:\\EventLog\\xml_op.xml");
br = new BufferedReader(new FileReader(inputFile));
String line="",con_line="";
int inc =0;
while((line = br.readLine())!=null)
{
con_line+=line;
}
System.out.println("Constructed Line = "+con_line);
Document doc=DocumentHelper.parseText(con_line);
System.out.println("Root Element = "+doc.getRootElement().getName());
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.element("Provider").attributeValue("Name"));
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.element("TimeCreated").attributeValue("SystemTime"));
System.out.println("EventRecordID--> " + Sys.elementText("EventRecordID"));
System.out.println("Channel--> " + Sys.elementText("Channel"));
System.out.println("Computer--> " + Sys.elementText("Computer"));
System.out.println("------------------------------------------------");
}
for (Iterator i1 = parent_ele.elementIterator("EventData"); i1.hasNext();)
{
Element Sys = (Element) i1.next();
System.out.println("Data--> " + Sys.elementText("Data"));
}
}
catch(Exception e)
{
e.printStackTrace();
}

这是我的输出:

Constructed Line = <Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'>    <System><Provider Name='Application Error'/>        <EventID Qualifiers='0'>1000</EventID>      <Level>2</Level>        <Task>100</Task>        <Keywords>0x80000000000000</Keywords>       <TimeCreated SystemTime='2015-07-29T04:33:30.000000000Z'/>      <EventRecordID>264</EventRecordID>      <Channel>Application</Channel>      <Computer>WIN-R8CUJUTBUMV</Computer>        <Security/> </System>   <EventData>     <Data>AppCrasher.exe</Data>     <Data>0.0.0.0</Data>        <Data>55b239c6</Data>       <Data>AppCrasher.exe</Data>     <Data>0.0.0.0</Data>        <Data>55b239c6</Data>       <Data>c0000005</Data>       <Data>000113e8</Data>       <Data>308</Data>        <Data>01d0c9b7b7373caa</Data>       <Data>C:\eGurkha\CrashLogTest_Script\AppCrasher x64\AppCrasher.exe</Data>       <Data>C:\eGurkha\CrashLogTest_Script\AppCrasher x64\AppCrasher.exe</Data>       <Data>f6135af5-35aa-11e5-b2fe-005056b27da4</Data>   </EventData></Event>
Root Element = Event
------------------------------------------------
Provider--> Application Error
EventID--> 1000
Level--> 2
Task--> 100
Keywords--> 0x80000000000000
TimeCreated--> 2015-07-29T04:33:30.000000000Z
EventRecordID--> 264
Channel--> Application
Computer--> WIN-R8CUJUTBUMV
------------------------------------------------
Data--> AppCrasher.exe // Here I couldn't get the complete data.

我在输出中已经提到,我需要一一获取完整的数据。我需要做什么?分享您的想法。

提前致谢。

最佳答案

您刚刚加载了第一个 Data 元素。您必须获取所有 Data 元素。

您需要做的是:

Element eventDataElement = (Element)parent_ele.getElementsByTagName("EventData").item(0);  // Load the first <EventData> element

NodeList listOfData = eventDataElement.getElementsByTagName("Data"); // Load all the elements with <Data> tag

// Iterate through each <Data> tag
for(int i = 0; i < listOfData.getLength(); i++) {

Element data = (Element)listOfData.item(i); // Get the i'th <Data> element

System.out.println("Data --> "+ data.getTextContent()); // Print the text content.
}

仅供引用

NodeList 类来自 org.w3c.dom 包。

关于java - 使用 dom4j 解析 xml 文件时无法获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31691867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com