Java XML NodeList parserXml(NodeList employees, Class class_type)

Here you can find the source of parserXml(NodeList employees, Class class_type)

Description

parser Xml

License

Apache License

Declaration

@SuppressWarnings({ "unchecked" })
    private static Object parserXml(NodeList employees, Class<? extends Map> class_type)
            throws DOMException, InstantiationException, IllegalAccessException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

import java.util.List;
import java.util.Map;

import org.w3c.dom.DOMException;

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

public class Main {
    @SuppressWarnings({ "unchecked" })
    private static Object parserXml(NodeList employees, Class<? extends Map> class_type)
            throws DOMException, InstantiationException, IllegalAccessException {
        Map map = class_type.newInstance();
        String value = null;/*from   w ww . ja v a2 s .  com*/
        for (int i = 0; i < employees.getLength(); i++) {
            Node employee = employees.item(i);
            if (employee.getNodeType() != 8)
                if (employee.getNodeType() == 1 || employee.getNodeValue() == null
                        || employee.getNodeValue().trim().isEmpty()) {
                    if (employee.hasChildNodes() || employee.hasAttributes()) {
                        Object sub_map = parserXml(employee.getChildNodes(), class_type);
                        String name = employee.getNodeName();
                        NamedNodeMap attributes = employee.getAttributes();
                        for (int attribute_index = 0; attribute_index < attributes.getLength(); attribute_index++) {
                            Node attribute = attributes.item(attribute_index);
                            String attribute_nameString = attribute.getNodeName().trim();
                            if (attribute_nameString.equalsIgnoreCase("name")) {
                                name = (attribute.getNodeValue() == null
                                        || attribute.getNodeValue().trim().isEmpty()) ? name
                                                : attribute.getNodeValue().trim();
                            } else {
                                if (sub_map instanceof Map) {
                                    ((Map) sub_map).put(attribute_nameString,
                                            (attribute.getNodeValue() == null
                                                    || attribute.getNodeValue().trim().isEmpty()) ? null
                                                            : attribute.getNodeValue().trim());
                                } else {
                                    String sub_value = (String) sub_map;
                                    sub_map = class_type.newInstance();
                                    ((Map) sub_map).put(name, sub_value);
                                    ((Map) sub_map).put(attribute_nameString,
                                            (attribute.getNodeValue() == null
                                                    || attribute.getNodeValue().trim().isEmpty()) ? null
                                                            : attribute.getNodeValue().trim());
                                }
                            }
                        }
                        if (sub_map != null) {
                            Object sub_map_list = map.get(name);
                            if (sub_map_list == null) {
                                map.put(name, sub_map);
                            } else {
                                if (!(sub_map_list instanceof List)) {
                                    sub_map_list = new ArrayList();
                                    ((List) sub_map_list).add(map.get(name));
                                    map.put(name, sub_map_list);
                                }
                                ((List) sub_map_list).add(sub_map);
                            }
                        }
                    }
                } else {
                    value = ((employee.getNodeValue().trim().isEmpty()) ? null : employee.getNodeValue().trim());
                }
        }
        if (map.size() <= 0 && value != null) {
            return value;
        } else {
            if (value != null) {
                map.put("", value);
            }
            return map;
        }
    }
}

Related

  1. listOf(final NodeList nodeList)
  2. listOf(final NodeList nodeList)
  3. mapNodeList(NodeList nodeList)
  4. mergeTextContent(final NodeList nodes)
  5. mergeTextContent(NodeList nodes)
  6. parseSwitcherConfigs(NodeList switcherConfigNodes)
  7. parseSwitchers(NodeList switcherNodes)
  8. parseWidget(NodeList widgetList)
  9. prettyPrintLoop(NodeList nodes, StringBuilder string, String indentation)