Android XML解析以计算空值的数目

Android XML解析以计算空值的数目,android,xml,Android,Xml,我是android新手。我需要解析XML来计算标记中空值的数量 例如,在下面的XML文件中有3本书,我想计算到期日期(tag 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:searchRetrieveRes

我是android新手。我需要解析XML来计算标记中空值的数量

例如,在下面的XML文件中有3本书,我想计算到期日期(tag 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>

但是它返回3,我希望它是2。

请查看下面的代码是否有帮助:

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){

    }
试试看{
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
//使用工厂创建documentbuilder
DocumentBuilder=factory.newDocumentBuilder();
//从输入源创建新文档
字符串响应=“\n”+
“1.1\n”+
“1\n”+
“\n”+
“\n”+
“dc\n”+
“xml\n”+
“\n”+
“\n”+
“\n”+
“知识产权时代的知识获取/\n”+
“\n”+
卡普钦斯基,艾米。\n+
“克里科里安,加列,1972-\n”+
“文本\n”+
纽约:区域图书,\n+
“2010。\n”+
“英文\n”+
“包括参考书目。\n”+
“知识产权。\n”+
“访问知识移动。\n”+
“信息自由。\n”+
“B1246855\n”+
“\n”+
“B1246854\n”+
“\n”+
“B1246853\n”+
“2015-12-31\n”+
“\n”+
“\n”+
“1\n”+
“\n”+
“\n”+
"";
InputSource src=新的InputSource();
src.setCharacterStream(新StringReader(响应));
文档doc=builder.parse(src);
NodeList nodes=doc.getElementsByTagName(“dc:duedate”);
//打印每个子级的文本内容
int-cnt=0;
对于(int i=0;i

在我身边工作。请看看是否有帮助。

有什么想法吗??让我知道欢迎。快乐编码!
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){

    }