Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.StringReader;

import javax.xml.xpath.*;
import org.xml.sax.InputSource;

public class Main {
    public static String getNodeCount(String nodeName, String xmlString) {
        String response = "";
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        try {
            XPathExpression xPathExpression = xPath.compile("count(//" + nodeName + ")");
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xmlString));
            response = xPathExpression.evaluate(is);

        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }

        return response;
    }
}