Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.FileInputStream;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static List<Map<String, String>> readXMLFile(String outFile) {
        DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
        List<Map<String, String>> returnlist = new ArrayList<Map<String, String>>();

        try {
            DocumentBuilder dombuilder = domfac.newDocumentBuilder();
            InputStream is = new FileInputStream(outFile);
            Document doc = dombuilder.parse(is);
            NodeList nl = doc.getElementsByTagName("row");
            for (int i = 0; i < nl.getLength(); i++) {
                Node node = nl.item(i);
                NodeList fileds = node.getChildNodes();
                Map<String, String> map = new HashMap<String, String>();
                for (int j = 0; j < fileds.getLength(); j++) {
                    Node filed = fileds.item(j);

                    if (filed.getNodeType() == Node.ELEMENT_NODE) {
                        map.put(filed.getAttributes().getNamedItem("name").getNodeValue(),
                                filed.getFirstChild().getNodeValue());
                    }
                }
                returnlist.add(map);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnlist;

    }
}