【发布时间】:2016-01-03 05:47:17
【问题描述】:
我是安卓新手。我需要解析 XML 来计算标签中空值的数量。
例如,下面的 XML 文件中有 3 本书,我想计算未提及到期日期(标签 dc:duedate/)的位置/标签为空,因此它返回结果为 2。有人可以建议吗?非常感谢。
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<zs:searchRetrieveResponse xmlns:zs="http://www.loc.gov/zing/srw/">
<zs:version>1.1</zs:version>
<zs:numberOfRecords>1</zs:numberOfRecords>
<zs:records>
<zs:record>
<zs:recordSchema>dc</zs:recordSchema>
<zs:recordPacking>xml</zs:recordPacking>
<zs:recordData>
<dc:dc xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>
Access to knowledge in the age of intellectual property /
</dc:title>
<dc:creator>Kapczynski, Amy.</dc:creator>
<dc:creator>Krikorian, Gaëlle, 1972-</dc:creator>
<dc:type>text</dc:type>
<dc:publisher>New York : Zone Books,</dc:publisher>
<dc:date>2010.</dc:date>
<dc:language>eng</dc:language>
<dc:description>Includes bibliographical references.</dc:description>
<dc:subject>Intellectual property.</dc:subject>
<dc:subject>Access to knowledge movement.</dc:subject>
<dc:subject>Freedom of information.</dc:subject>
<dc:barcode>B1246855</dc:barcode>
<dc:duedate/>
<dc:barcode>B1246854</dc:barcode>
<dc:duedate/>
<dc:barcode>B1246853</dc:barcode>
<dc:duedate>2015-12-31</dc:duedate>
</dc:dc>
</zs:recordData>
<zs:recordPosition>1</zs:recordPosition>
</zs:record>
</zs:records>
</zs:searchRetrieveResponse>
我正在使用
DocumentBuilder builder = null;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource src = new InputSource();
src.setCharacterStream(new StringReader(response));
Document doc = builder.parse(src);
NodeList list = doc.getElementsByTagName("dc:duedate");
Toast.makeText(getActivity().getApplicationContext(), "Total : " + list.getLength()
,Toast.LENGTH_LONG).show();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
但它返回 3,我希望它是 2。
【问题讨论】:
-
有什么想法吗??告诉我