2013-05-29 54 views
0

我很難從JAVA中讀取來自solr的XML,現在我使用文檔和節點列表來解析XML,我需要讀取節點的每個值,並且需要讀取最後一個值eventID。這是我的Solr的XML結果:在java中讀取XML屬性

<?xml version="1.0" encoding="UTF-8"?> 
<response> 

<lst name="responseHeader"> 
<int name="status">0</int> 
<int name="QTime">338</int> 
</lst> 
<result name="response" numFound="2" start="0"> 
<doc> 
<long name="ID">4</long> 
<int name="SourceID">2</int> 
<str name="SourceTitle">EIT</str> 
<str name="SourceDescription">ethichal intelligent technologies</str> 
<str name="Active">1</str> 
<date name="CreateDate">2006-01-17T00:00:00Z</date> 
<str name="_version_">1436370186807541760</str></doc> 
<doc> 
<long name="ID">5</long> 
<int name="SourceID">2</int> 
<str name="SourceTitle">EIT</str> 
<str name="SourceDescription">ethichal intelligent technologies</str> 
<str name="Active">1</str> 
<date name="CreateDate">2006-01-17T00:00:00Z</date> 
<str name="_version_">1436370268221079552</str></doc> 
</result> 
</response> 

在此我要讀的SourceID,SourceTitle和需要閱讀SourceDescription的最後一個屬性。 請幫幫我,怎麼弄的SourceID值(意思2),SourceTiltle值(意思EIT)這樣的..

+0

SourceDescription沒有屬性,所以什麼是你想讀什麼書?您目前對您的代碼有什麼問題 - 即我們可以幫助您調試代碼來發現問題。還有,你有沒有使用SolrJ之類的原因? – Swati

+0

到目前爲止你試過了什麼? – Sanghita

+0

在這個特定的行 2,如何讀取值,我的意思是屬性爲SourceID,我沒有使用solrj,我得到了XML但我不知道如何提取XML –

回答

0

假設你想要得到的價值 「的SourceID」:

Element el = (get to the element named int); 
String value = el.getAttribute("name"); 
// value should equals "SourceID" 

如果你真正想要的是2號,用途:

String content = el.getTextContent(); 
// and then convert to integer, or whatever 
+0

感謝您的回答,我仍然與此鬥爭,有沒有可能通過像ID,SourceID,SourceDescription得到的價值,請給我一步一步的過程, –

+0

我不確定你在問什麼......如果你問「獲取SourceID的值,其中ID = ,你需要使用XPath查找,這是正確的方法(和更好地閱讀一個簡短的教程,而不是試圖在這裏解釋)。 – user1676075