Java tutorial
//package com.java2s; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; public class Main { public static int getNodeSizeFromXmlStr(String xmlStr, String nodeName) { int size = 0; xmlStr = xmlStr.trim(); Element element = getRootNodeFromXmlStr(xmlStr); size = element.getElementsByTagName(nodeName).getLength(); return size; } private static Element getRootNodeFromXmlStr(String xmlStr) { Element element = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputStream is = new ByteArrayInputStream(xmlStr.getBytes("UTF-8")); Document document = db.parse(is); element = document.getDocumentElement(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return element; } }