我是android新手。我需要解析XML来计算标记中空值的数量。
例如,下面的XML文件中有3本书,我想计算未提及截止日期(标签dc:duedate /)/ tag是空的,因此它将结果返回为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。
答案 0 :(得分:0)
请查看以下代码是否有帮助:
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// use the factory to create a documentbuilder
DocumentBuilder builder = factory.newDocumentBuilder();
// create a new document from input source
String response="<zs:searchRetrieveResponse xmlns:zs=\"http://www.loc.gov/zing/srw/\">\n" +
"<zs:version>1.1</zs:version>\n" +
"<zs:numberOfRecords>1</zs:numberOfRecords>\n" +
"<zs:records>\n" +
"<zs:record>\n" +
"<zs:recordSchema>dc</zs:recordSchema>\n" +
"<zs:recordPacking>xml</zs:recordPacking>\n" +
"<zs:recordData>\n" +
"<dc:dc xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n" +
"<dc:title>\n" +
"Access to knowledge in the age of intellectual property /\n" +
"</dc:title>\n" +
"<dc:creator>Kapczynski, Amy.</dc:creator>\n" +
"<dc:creator>Krikorian, Gaëlle, 1972-</dc:creator>\n" +
"<dc:type>text</dc:type>\n" +
"<dc:publisher>New York : Zone Books,</dc:publisher>\n" +
"<dc:date>2010.</dc:date>\n" +
"<dc:language>eng</dc:language>\n" +
"<dc:description>Includes bibliographical references.</dc:description>\n" +
"<dc:subject>Intellectual property.</dc:subject>\n" +
"<dc:subject>Access to knowledge movement.</dc:subject>\n" +
"<dc:subject>Freedom of information.</dc:subject>\n" +
"<dc:barcode>B1246855</dc:barcode>\n" +
"<dc:duedate/>\n" +
"<dc:barcode>B1246854</dc:barcode>\n" +
"<dc:duedate/>\n" +
"<dc:barcode>B1246853</dc:barcode>\n" +
"<dc:duedate>2015-12-31</dc:duedate>\n" +
"</dc:dc>\n" +
"</zs:recordData>\n" +
"<zs:recordPosition>1</zs:recordPosition>\n" +
"</zs:record>\n" +
"</zs:records>\n" +
"</zs:searchRetrieveResponse>";
InputSource src = new InputSource();
src.setCharacterStream(new StringReader(response));
Document doc = builder.parse(src);
NodeList nodes = doc.getElementsByTagName("dc:duedate");
// print the text content of each child
int cnt=0;
for (int i = 0; i < nodes.getLength(); i++) {
if(nodes.item(i).getTextContent().trim().isEmpty()){
cnt++;
}
}
Log.e("TAGLOG","" + cnt);
}catch (Exception e){
}
在我身边工作。请看看它是否有帮助。