Java XML Parse Stream parseXML(InputStream is, String[] tagNames)

Here you can find the source of parseXML(InputStream is, String[] tagNames)

Description

parse XML

License

Apache License

Declaration

public static Map<String, String> parseXML(InputStream is,
            String[] tagNames) 

Method Source Code

//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;
    }
}

Related

  1. parseXml(InputStream in)
  2. parseXml(InputStream in)
  3. parseXML(InputStream in)
  4. parseXml(InputStream inputStream, boolean validating)
  5. parseXML(InputStream is)
  6. parseXml(InputStream src)
  7. parseXMLFromStream(InputStream stream)
  8. parseXmlInputStream(InputStream inputStream)
  9. parseXmlInputStream(InputStream inputStream)