Here you can find the source of parseXML(InputStream is, String[] tagNames)
public static Map<String, String> parseXML(InputStream is, String[] tagNames)
//package com.java2s; // Licensed to the Apache Software Foundation (ASF) under one import java.io.InputStream; import java.util.HashMap; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class Main { private static DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); public static Map<String, String> parseXML(InputStream is, String[] tagNames) {// w w w . j ava2 s . c o m Map<String, String> returnValues = new HashMap<String, String>(); try { DocumentBuilder docBuilder = factory.newDocumentBuilder(); Document doc = docBuilder.parse(is); Element rootElement = doc.getDocumentElement(); for (int i = 0; i < tagNames.length; i++) { NodeList targetNodes = rootElement .getElementsByTagName(tagNames[i]); if (targetNodes.getLength() <= 0) { System.out.println("no " + tagNames[i] + " tag in the response"); returnValues.put(tagNames[i], null); } else { returnValues.put(tagNames[i], targetNodes.item(0) .getTextContent()); } } } catch (Exception ex) { System.out.println("error processing XML"); ex.printStackTrace(); } return returnValues; } }