java中的XML属性读取

时间:2013-05-29 15:30:59

标签: java xml solr

我很难读取来自JAVA的solr结果的XML,现在我使用文档和节点列表来解析XML,我需要读取节点的每个值,我需要读取eventID的最后一个值。这是我在Solr的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)就是这样..

1 个答案:

答案 0 :(得分: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