Example usage for org.dom4j Element elementIterator

List of usage examples for org.dom4j Element elementIterator

Introduction

In this page you can find the example usage for org.dom4j Element elementIterator.

Prototype

Iterator<Element> elementIterator();

Source Link

Document

Returns an iterator over all this elements child elements.

Usage

From source file:com.kingmed.yuyt.util.XMLHandler.java

public static Map<String, String> transSimpleXmltoMap(String xmlInfo) throws DocumentException {
    Document doc = DocumentHelper.parseText(xmlInfo); // xml
    Map<String, String> map = new HashMap<String, String>();
    Element rootElt = doc.getRootElement();
    Iterator iter = rootElt.elementIterator();
    while (iter.hasNext()) {
        Element dataElement = (Element) iter.next();
        map.put(dataElement.getName(), dataElement.getText());
    }//from  w  w w. j  a v a  2  s  .c  om
    return map;
}

From source file:com.mobileStore.sms.CCPRestSmsSDK.java

License:Open Source License

/**
 * @description xml??map/*from   w w w.ja  v a2  s .  c o m*/
 * @param xml
 * @return Map
 */
private static HashMap<String, Object> xmlToMap(String xml) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    Document doc = null;
    try {
        doc = DocumentHelper.parseText(xml); // XML
        Element rootElt = doc.getRootElement(); // ?
        HashMap<String, Object> hashMap2 = new HashMap<String, Object>();
        for (Iterator i = rootElt.elementIterator(); i.hasNext();) {
            Element e = (Element) i.next();
            if ("statusCode".equals(e.getName()) || "statusMsg".equals(e.getName()))
                map.put(e.getName(), e.getText());
            else {
                if ("SubAccount".equals(e.getName()) || "totalCount".equals(e.getName())
                        || "token".equals(e.getName()) || "downUrl".equals(e.getName())) {
                    if (!"SubAccount".equals(e.getName())) {
                        hashMap2.put(e.getName(), e.getText());
                    } else {
                        ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
                        HashMap<String, Object> hashMap3 = new HashMap<String, Object>();
                        for (Iterator i2 = e.elementIterator(); i2.hasNext();) {
                            Element e2 = (Element) i2.next();
                            hashMap3.put(e2.getName(), e2.getText());
                            arrayList.add(hashMap3);
                        }
                        hashMap2.put("SubAccount", arrayList);
                    }
                    map.put("data", hashMap2);
                } else {

                    HashMap<String, Object> hashMap3 = new HashMap<String, Object>();
                    for (Iterator i2 = e.elementIterator(); i2.hasNext();) {
                        Element e2 = (Element) i2.next();
                        // hashMap2.put(e2.getName(),e2.getText());
                        hashMap3.put(e2.getName(), e2.getText());
                    }
                    if (hashMap3.size() != 0) {
                        hashMap2.put(e.getName(), hashMap3);
                    } else {
                        hashMap2.put(e.getName(), e.getText());
                    }
                    map.put("data", hashMap2);
                }
            }
        }
    } catch (DocumentException e) {
        e.printStackTrace();
        LoggerUtil.error(e.getMessage());
    } catch (Exception e) {
        LoggerUtil.error(e.getMessage());
        e.printStackTrace();
    }
    return map;
}

From source file:com.mpaike.core.config.xml.elementreader.GenericElementReader.java

License:Open Source License

/**
 * Recursively processes the children creating the required config element
 * objects as it goes//ww  w  .  j  a va 2 s.  c  om
 * 
 * @param element
 * @param parentConfig
 */
@SuppressWarnings("unchecked")
private void processChildren(Element element, GenericConfigElement parentConfig) {
    // get the list of children for the given element
    Iterator<Element> children = element.elementIterator();
    while (children.hasNext()) {
        Element child = children.next();
        GenericConfigElement childConfigElement = createConfigElement(child);
        parentConfig.addChild(childConfigElement);

        // recurse down the children
        processChildren(child, childConfigElement);
    }
}

From source file:com.mpaike.core.config.xml.XMLConfigService.java

License:Open Source License

/**
 * Parses the evaluators element/*from w w w  . j a  v a 2  s  .  c  o m*/
 * 
 * @param evaluatorsElement
 */
private Map<String, Evaluator> parseEvaluatorsElement(Element evaluatorsElement) {
    if (evaluatorsElement != null) {
        Map<String, Evaluator> parsedEvaluators = new HashMap<String, Evaluator>();
        @SuppressWarnings("unchecked")
        Iterator<Element> evaluators = evaluatorsElement.elementIterator();
        while (evaluators.hasNext()) {
            Element evaluatorElement = evaluators.next();
            String evaluatorName = evaluatorElement.attributeValue(ATTR_ID);
            String evaluatorClass = evaluatorElement.attributeValue(ATTR_CLASS);

            // TODO: Can these checks be removed if we use a DTD and/or
            // schema??
            if (evaluatorName == null || evaluatorName.length() == 0) {
                throw new ConfigException("All evaluator elements must define an id attribute");
            }

            if (evaluatorClass == null || evaluatorClass.length() == 0) {
                throw new ConfigException("Evaluator '" + evaluatorName + "' must define a class attribute");
            }

            // add the evaluator
            parsedEvaluators.put(evaluatorName, createEvaluator(evaluatorName, evaluatorClass));
        }

        return parsedEvaluators;
    }

    return null;
}

From source file:com.mpaike.core.config.xml.XMLConfigService.java

License:Open Source License

/**
 * Parses the element-readers element//from w  w  w.  j a  v a  2s  .c om
 * 
 * @param readersElement
 */
private Map<String, ConfigElementReader> parseElementReadersElement(Element readersElement) {
    if (readersElement != null) {
        Map<String, ConfigElementReader> parsedElementReaders = new HashMap<String, ConfigElementReader>();
        @SuppressWarnings("unchecked")
        Iterator<Element> readers = readersElement.elementIterator();
        while (readers.hasNext()) {
            Element readerElement = readers.next();
            String readerElementName = readerElement.attributeValue(ATTR_ELEMENT_NAME);
            String readerElementClass = readerElement.attributeValue(ATTR_CLASS);

            if (readerElementName == null || readerElementName.length() == 0) {
                throw new ConfigException("All element-reader elements must define an element-name attribute");
            }

            if (readerElementClass == null || readerElementClass.length() == 0) {
                throw new ConfigException(
                        "Element-reader '" + readerElementName + "' must define a class attribute");
            }

            // add the element reader
            parsedElementReaders.put(readerElementName,
                    createConfigElementReader(readerElementName, readerElementClass));
        }

        return parsedElementReaders;
    }

    return null;
}

From source file:com.mpaike.core.config.xml.XMLConfigService.java

License:Open Source License

/**
 * Parses a config element of a config file
 * //  w  w w. j  a v  a  2 s  .c o m
 * @param configElement The config element
 * @param currentArea The current area
 */
private ConfigSection parseConfigElement(Map<String, ConfigElementReader> parsedElementReaders,
        Element configElement, String currentArea) {
    if (configElement != null) {
        boolean replace = false;
        String evaluatorName = configElement.attributeValue(ATTR_EVALUATOR);
        String condition = configElement.attributeValue(ATTR_CONDITION);
        String replaceValue = configElement.attributeValue(ATTR_REPLACE);
        if (replaceValue != null && replaceValue.equalsIgnoreCase("true")) {
            replace = true;
        }

        // create the section object
        ConfigSectionImpl section = new ConfigSectionImpl(evaluatorName, condition, replace);

        // retrieve the config elements for the section
        @SuppressWarnings("unchecked")
        Iterator<Element> children = configElement.elementIterator();
        while (children.hasNext()) {
            Element child = children.next();
            String elementName = child.getName();

            // get the element reader for the child
            ConfigElementReader elementReader = null;
            if (parsedElementReaders != null) {
                elementReader = parsedElementReaders.get(elementName);
            }

            if (elementReader == null) {
                elementReader = getConfigElementReader(elementName);
            }

            if (logger.isDebugEnabled())
                logger.debug("Retrieved element reader " + elementReader + " for element named '" + elementName
                        + "'");

            if (elementReader == null) {
                elementReader = new GenericElementReader(propertyConfigurer);

                if (logger.isDebugEnabled())
                    logger.debug("Defaulting to " + elementReader + " as there wasn't an element "
                            + "reader registered for element '" + elementName + "'");
            }

            ConfigElement cfgElement = elementReader.parse(child);
            section.addConfigElement(cfgElement);

            if (logger.isDebugEnabled())
                logger.debug("Added " + cfgElement + " to " + section);
        }

        return section;
    }

    return null;
}

From source file:com.neu.utils.io.NetworkToXMLUtil.java

License:Open Source License

public static List<List<String>> unicomFilmXmlRead(String fileName) throws DocumentException {

    List<List<String>> cc = new ArrayList<List<String>>();

    File inputXml = new File(fileName);
    SAXReader saxReader = new SAXReader();

    Document document = saxReader.read(inputXml);

    Element employees = document.getRootElement();
    for (Iterator i = employees.elementIterator(); i.hasNext();) {
        Element employee = (Element) i.next();
        List<String> list = new ArrayList<String>();
        for (Iterator j = employee.elementIterator(); j.hasNext();) {
            Element node = (Element) j.next();
            list.add(node.getText());/*from w w w .  j  a  va 2  s  .c o  m*/
        }
        cc.add(list);
    }
    return cc;
}

From source file:com.noterik.bart.fs.action.FlandersAction.java

License:Open Source License

private String processXml(String original, String flanders) {
    String xml = "";

    Map<String, String> values = new HashMap<String, String>();

    Document origdoc = null;/* w w w  . j  a va2 s  .c om*/
    Document flandoc = null;

    try {
        origdoc = DocumentHelper.parseText(original);
        flandoc = DocumentHelper.parseText(flanders);
    } catch (DocumentException e) {
        logger.error("", e);
    }

    Element origProp = (Element) origdoc.selectSingleNode("//properties");
    Iterator i = origProp.elementIterator();

    while (i.hasNext()) {
        Element prop = (Element) i.next();
        String name = prop.getName();
        String value = prop.getText();
        values.put(name, value);
    }

    logger.debug("\n flandProp = " + flandoc.asXML());

    Element flandProp = (Element) flandoc.selectSingleNode("/meta-data");
    Iterator j = flandProp.elementIterator();

    while (j.hasNext()) {
        Element prop = (Element) j.next();
        String name = prop.getName();
        String value = prop.getText();

        //For marin there metadata is leading
        if (values.containsKey("mount") && values.get("mount").toLowerCase().equals("marin")) {
            if (!values.containsKey(name)) {
                values.put(name, value);
            }
        } else {
            values.put(name, value);
        }
    }

    Element finalEl = DocumentHelper.createElement("fsxml");
    Element propsEl = finalEl.addElement("properties");

    Iterator<String> it = values.keySet().iterator();
    while (it.hasNext()) {
        String name = it.next();
        String value = values.get(name);
        propsEl.addElement(name).addText(value);
    }

    xml = finalEl.asXML();

    return xml;
}

From source file:com.noterik.bart.fs.fscommand.CommandAdapter.java

License:Open Source License

/**
 * Returns the input parameters.//from   ww w. j  a  v  a2s  .  c om
 * 
 * @param xml   The xml specifying the commands parameters.
 * @return      The input parameters.
 */
public Properties getInputParameters(String xml) {
    Properties props = new Properties();
    Document doc = XMLHelper.asDocument(xml);
    if (doc == null) {
        return null;
    } else {
        Node n = doc.selectSingleNode("./fsxml/properties");
        if (n instanceof Element) {
            Element properties = (Element) n;
            for (Iterator i = properties.elementIterator(); i.hasNext();) {
                Element elem = (Element) i.next();
                props.put(elem.getName(), elem.getText().trim());
            }
        }
    }
    return props;
}

From source file:com.noterik.bart.fs.fscommand.ShiftCommand.java

License:Open Source License

/**
 * Returns a map of events that belong to the given presentation uri
 * @param uri//www  . j  a v a2  s.  c o  m
 * @return
 */
private Map<String, String> getEvents(String uri) {
    logger.debug("getting events for presentation: " + uri);
    Map<String, String> map = new HashMap<String, String>();
    Document doc = FSXMLRequestHandler.instance().getNodeProperties(uri, false);
    try {
        // get videoplaylist
        Element videoplaylist = (Element) doc.selectSingleNode("//videoplaylist[@id='1']");

        // get all child nodes
        Element elem, propElem;
        String id, name, eUri, properties;
        for (Iterator<Element> iter = videoplaylist.elementIterator(); iter.hasNext();) {
            try {
                // get element
                elem = iter.next();

                // get id, name, uri and properties
                id = elem.valueOf("@id");
                name = elem.getName();
                propElem = (Element) elem.selectSingleNode("properties");
                properties = propElem != null ? propElem.asXML() : null;
                eUri = uri + "/videoplaylist/1/" + name + "/" + id;
                logger.debug("id: " + id + ", name: " + name + ", eventUri: " + eUri + ", properties: "
                        + properties);

                // check to exclude elements
                if (EXCLUDE_ELEMENTS.contains(name)) {
                    continue;
                }

                // add to map
                map.put(eUri, properties);
            } catch (Exception e) {
                logger.error("", e);
            }
        }
    } catch (Exception e) {
        logger.error("", e);
    }
    return map;
}