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

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

Description

parse Mul XML

License

Apache License

Declaration

public static ArrayList<HashMap<String, String>> parseMulXML(
            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.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    private static DocumentBuilderFactory factory = DocumentBuilderFactory
            .newInstance();/*  ww  w .j a  va2s  . c  o m*/

    public static ArrayList<HashMap<String, String>> parseMulXML(
            InputStream is, String[] tagNames) {
        ArrayList<HashMap<String, String>> returnValues = new ArrayList<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 XML response...returning null");
                } else {
                    for (int j = 0; j < targetNodes.getLength(); j++) {
                        HashMap<String, String> valueList = new HashMap<String, String>();
                        Node node = targetNodes.item(j);
                        //parse child nodes
                        NodeList child = node.getChildNodes();
                        for (int c = 0; c < node.getChildNodes()
                                .getLength(); c++) {
                            child.item(c).getNodeName();
                            valueList.put(child.item(c).getNodeName(),
                                    child.item(c).getTextContent());
                        }
                        returnValues.add(valueList);
                    }

                }
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }

        return returnValues;
    }
}

Related

  1. parse(XMLStreamReader reader)
  2. parse(XMLStreamReader reader, String... elementNames)
  3. parseDoc(final InputStream is)
  4. parseInputStream(InputStream in, boolean namespaces, boolean validating)
  5. parseInputStream(InputStream is)
  6. parseNamespace(XMLStreamReader reader)
  7. parseNoContent(final XMLStreamReader reader)
  8. parseNumber(XMLEventReader stream)
  9. parser(InputStream inputStream)