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(QName qName);

Source Link

Document

Returns an iterator over the elements contained in this element which match the given fully qualified name.

Usage

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 * ???/*from  w  ww. j  a v  a 2  s . c  o  m*/
 *
 * @param element
 *            
 * @param elementName?
 * @return?
 */
public Iterator<Object> elementIterator(Element element, String elementName) {
    @SuppressWarnings("unchecked")
    Iterator<Object> elemIterator = element.elementIterator(elementName);
    return elemIterator;
}

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 * ?//  w w  w  .j  av a  2  s. com
 *
 * @param document
 * @param NodeName??????//???
 *            //articlearticle
 * @param element
 *            ???
 */
public void setElementValue(Document document, String NodeName, String elementName, String elementValue) {
    List<?> list = document.selectNodes(NodeName);
    Iterator<?> iter = list.iterator();
    while (iter.hasNext()) {
        Element element = (Element) iter.next();
        Iterator<?> iterator = element.elementIterator(elementName);
        while (iterator.hasNext()) {
            Element titleElement = (Element) iterator.next();
            titleElement.setText(elementValue);
        }
    }

}

From source file:com.hwsoft.util.idcard.IDCardVerifyTools.java

/**
 * ?Nciicl?/*  ww  w  .  j  a v  a  2  s. c  o  m*/
 *
 * @param backXMl?
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static IdCardInfo parseXml(String backXMl, String userName, String idCard) {
    Document doc = null;
    String result_gmsfhm = null;
    String result_xm = null;
    String gmsfhm = null;
    String xm = null;
    String xp = null;
    String errormesage = null;
    IdCardInfo IdCardInfo = null;
    System.out.println("idcard bind Nciic  backInfo:" + backXMl);

    try {
        //XML
        doc = DocumentHelper.parseText(backXMl);

        // ?
        Element rootElt = doc.getRootElement();

        if ("RESPONSE".equalsIgnoreCase(rootElt.getName())) {
            //???
            List<DefaultAttribute> list = rootElt.attributes();

            for (DefaultAttribute e : list) {
                if ("errorcode".equals(e.getName())) {
                    if ("-70".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("?", null);
                    } else if ("-71".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("?", null);
                    } else if ("-72".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("IP ???", null);
                    } else if ("-80".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("??", null);
                    }
                }
            }
        } else if ("ROWS".equalsIgnoreCase(rootElt.getName())) {
            Iterator iterForRow = rootElt.elementIterator("ROW");

            while (iterForRow.hasNext()) {
                Element recordEleForRow = (Element) iterForRow.next();

                //?????
                Iterator itersForInput = recordEleForRow.elementIterator("INPUT");

                while (itersForInput.hasNext()) {
                    Element recordEleForInput = (Element) itersForInput.next();
                    gmsfhm = recordEleForInput.elementTextTrim("gmsfhm");

                    if (!(idCard.equalsIgnoreCase(gmsfhm))) {
                        throw new RuntimeException("?????", null);
                    }

                    xm = recordEleForInput.elementTextTrim("xm");

                    if (!(userName.equalsIgnoreCase(xm))) {
                        throw new RuntimeException("?????", null);
                    }
                }

                //??????
                Iterator itersForOutput = recordEleForRow.elementIterator("OUTPUT");

                while (itersForOutput.hasNext()) {
                    Element recordEleForSubRow = (Element) itersForOutput.next();
                    Iterator itersForItem = recordEleForSubRow.elementIterator("ITEM");

                    while (itersForItem.hasNext()) {
                        Element recordEleForItem = (Element) itersForItem.next();
                        gmsfhm = recordEleForItem.elementTextTrim("gmsfhm");
                        xm = recordEleForItem.elementTextTrim("xm");
                        xp = recordEleForItem.elementTextTrim("xp");
                        errormesage = recordEleForItem.elementTextTrim("errormesage");

                        if (gmsfhm != null) {
                            result_gmsfhm = recordEleForItem.elementTextTrim("result_gmsfhm");
                        }

                        if (xm != null) {
                            result_xm = recordEleForItem.elementTextTrim("result_xm");
                        }

                        if (xp != null) {
                            xp = recordEleForItem.elementTextTrim("xp");
                        }

                        if (errormesage != null) {
                            errormesage = recordEleForItem.elementTextTrim("errormesage");
                            break;
                        }
                    }
                }

                if (null != errormesage) {
                    throw new RuntimeException("??!", null);
                } else {
                    if (!"".equalsIgnoreCase(result_xm)) {
                        throw new RuntimeException("?????", null);
                    }

                    if (!"".equalsIgnoreCase(result_gmsfhm)) {
                        throw new RuntimeException("?????", null);
                    }

                    IdCardInfo = new IdCardInfo();
                    IdCardInfo.setIdCardNumber(idCard);
                    IdCardInfo.setRealName(userName);
                    IdCardInfo.setAvatar(xp);
                }
            }
        }
    } catch (DocumentException e) {
        System.out.println("idcard bind erroinfo:XML??");
    } catch (Exception e) {
        System.out.println("idcard bind erroinfo:XML??");
    }
    return IdCardInfo;
}

From source file:com.hyron.poscafe.util.ConfigurationWithWildcard.java

License:Open Source License

private void addProperties(Element parent) {
    Properties props = new Properties();
    Iterator itr = parent.elementIterator("property");
    while (itr.hasNext()) {
        Element node = (Element) itr.next();
        String name = node.attributeValue("name");
        String value = node.getText().trim();
        log.debug(name + "=" + value);
        props.setProperty(name, value);// w w w.ja v  a  2  s  .co m
        if (!name.startsWith("hibernate")) {
            props.setProperty("hibernate." + name, value);
        }
    }
    addProperties(props);
    Environment.verifyProperties(getProperties());
}

From source file:com.jaspersoft.jasperserver.export.ImporterImpl.java

License:Open Source License

protected void process() {
    Document indexDocument = readIndexDocument();
    Element indexRoot = indexDocument.getRootElement();

    Properties properties = new Properties();
    for (Iterator it = indexRoot.elementIterator(getPropertyElementName()); it.hasNext();) {
        Element propElement = (Element) it.next();
        String propKey = propElement.attribute(getPropertyNameAttribute()).getValue();
        Attribute valueAttr = propElement.attribute(getPropertyValueAttribute());
        String value = valueAttr == null ? null : valueAttr.getValue();
        properties.setProperty(propKey, value);
    }/*w  w  w.ja va  2 s. c om*/
    input.propertiesRead(properties);

    Attributes contextAttributes = createContextAttributes();
    contextAttributes.setAttribute("sourceJsVersion", properties.getProperty(VERSION_ATTR));
    contextAttributes.setAttribute("targetJsVersion", super.getJsVersion());

    for (Iterator it = indexRoot.elementIterator(getIndexModuleElementName()); it.hasNext();) {
        if (Thread.interrupted()) {
            throw new RuntimeException("Cancelled");
        }
        Element moduleElement = (Element) it.next();
        String moduleId = moduleElement.attribute(getIndexModuleIdAttributeName()).getValue();
        ImporterModule module = getModuleRegister().getImporterModule(moduleId);
        if (module == null) {
            throw new JSException("jsexception.import.module.not.found", new Object[] { moduleId });
        }

        commandOut.debug("Invoking module " + module);

        contextAttributes.setAttribute("appContext", this.task.getApplicationContext());
        ModuleContextImpl moduleContext = new ModuleContextImpl(moduleElement, contextAttributes);
        module.init(moduleContext);

        List<String> messages = new ArrayList<String>();
        ImportRunMonitor.start();
        try {
            List<String> moduleMessages = module.process();
            if (moduleMessages != null) {
                messages.addAll(moduleMessages);
            }
        } finally {
            ImportRunMonitor.stop();
            for (String message : messages) {
                commandOut.info(message);
            }
        }
    }
}

From source file:com.jeeframework.util.xml.XMLProperties.java

License:Open Source License

/**
 * Return all values who's path matches the given property
 * name as a String array, or an empty array if the if there
 * are no children. This allows you to retrieve several values
 * with the same property name. For example, consider the
 * XML file entry://from  w w  w.j a v a 2  s .c o m
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 * If you call getProperties("foo.bar.prop") will return a string array containing
 * {"some value", "other value", "last value"}.
 *
 * @param name the name of the property to retrieve
 * @return all child property values for the given node name.
 */
public List<String> getProperties(String name, boolean asList) {
    List<String> result = new ArrayList<String>();
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        element = element.element(propName[i]);
        if (element == null) {
            // This node doesn't match this part of the property name which
            // indicates this property doesn't exist so return empty array.
            return result;
        }
    }
    // We found matching property, return names of children.
    Iterator<Element> iter = element.elementIterator(propName[propName.length - 1]);
    Element prop;
    String value;
    boolean updateEncryption = false;
    while (iter.hasNext()) {
        prop = iter.next();
        // Empty strings are skipped.
        value = prop.getTextTrim();
        if (!"".equals(value)) {
            // check to see if the property is marked as encrypted
            Attribute encrypted = prop.attribute(ENCRYPTED_ATTRIBUTE);
            if (encrypted != null) {
                value = EncryptUtil.desDecrypt(encryptKey, value);
            } else {
                // rewrite property as an encrypted value
                prop.addAttribute(ENCRYPTED_ATTRIBUTE, "true");
                updateEncryption = true;
            }
            result.add(value);
        }
    }
    if (updateEncryption) {
        Log.info("Rewriting values for XML property " + name + " using encryption");
        saveProperties();
    }
    return result;
}

From source file:com.jeeframework.util.xml.XMLProperties.java

License:Open Source License

/**
 * Return all values who's path matches the given property
 * name as a String array, or an empty array if the if there
 * are no children. This allows you to retrieve several values
 * with the same property name. For example, consider the
 * XML file entry:/*from  w w w  .ja  va  2s  .c  om*/
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 * If you call getProperties("foo.bar.prop") will return a string array containing
 * {"some value", "other value", "last value"}.
 *
 * @param name the name of the property to retrieve
 * @return all child property values for the given node name.
 */
public Iterator getChildProperties(String name) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        element = element.element(propName[i]);
        if (element == null) {
            // This node doesn't match this part of the property name which
            // indicates this property doesn't exist so return empty array.
            return Collections.EMPTY_LIST.iterator();
        }
    }
    // We found matching property, return values of the children.
    Iterator<Element> iter = element.elementIterator(propName[propName.length - 1]);
    ArrayList<String> props = new ArrayList<String>();
    Element prop;
    String value;
    while (iter.hasNext()) {
        prop = iter.next();
        value = prop.getText();
        // check to see if the property is marked as encrypted
        if (Boolean.parseBoolean(prop.attribute(ENCRYPTED_ATTRIBUTE).getText())) {
            value = EncryptUtil.desDecrypt(encryptKey, value);
        }
        props.add(value);
    }
    return props.iterator();
}

From source file:com.jeeframework.util.xml.XMLProperties.java

License:Open Source License

/**
 * Sets a property to an array of values. Multiple values matching the same property
 * is mapped to an XML file as multiple elements containing each value.
 * For example, using the name "foo.bar.prop", and the value string array containing
 * {"some value", "other value", "last value"} would produce the following XML:
 * <pre>/*  w w w. ja v  a 2s . c  om*/
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 *
 * @param name   the name of the property.
 * @param values the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values, boolean isEncrypt) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML hierarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<Element>();
    Iterator<Element> iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add(iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            Iterator<Node> it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            String propValue = StringEscapeUtils.escapeXml(value);
            // check to see if the property is marked as encrypted
            if (isEncrypt) {
                propValue = EncryptUtil.desEncrypt(encryptKey, value);
                childElement.addAttribute(ENCRYPTED_ATTRIBUTE, "true");
            }
            childElement.setText(propValue);
        }
    }
    saveProperties();

}

From source file:com.l2jfree.loginserver.dao.impl.GameserversDAOXml.java

License:Open Source License

/**
 * Load server name from xml/*from  w w w  .j  a  v a2 s.c om*/
 */
public GameserversDAOXml() {
    InputStream in = null;
    try {
        try {
            in = new FileInputStream("servername.xml");
        } catch (FileNotFoundException e) {
            // just for eclipse development, we have to search in dist folder
            in = new FileInputStream("dist/servername.xml");
        }

        SAXReader reader = new SAXReader();
        Document document = reader.read(in);

        Element root = document.getRootElement();

        // Find all servers_list (should have only one)
        for (Iterator<?> i = root.elementIterator("server"); i.hasNext();) {
            Element server = (Element) i.next();
            Integer id = null;
            String name = null;
            // For each server, read the attributes
            for (Iterator<?> iAttr = server.attributeIterator(); iAttr.hasNext();) {
                Attribute attribute = (Attribute) iAttr.next();
                if (attribute.getName().equals("id")) {
                    id = new Integer(attribute.getValue());
                } else if (attribute.getName().equals("name")) {
                    name = attribute.getValue();
                }
            }
            if (id != null && name != null) {
                Gameservers gs = new Gameservers();
                gs.setServerId(id);
                gs.setServerName(name);
                serverNames.put(id, gs);
            }
        }
        _log.info("Loaded " + serverNames.size() + " server names");
    } catch (FileNotFoundException e) {
        _log.warn("servername.xml could not be loaded : " + e.getMessage(), e);
    } catch (DocumentException e) {
        _log.warn("servername.xml could not be loaded : " + e.getMessage(), e);
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.context.GeoEventDispatcher.java

License:Apache License

static Message buildGeoMessageFromPubSubIQ(final IQ geoIQ) {
    Message geoMessage = null;//from   w w  w .  j  a v a2s .co  m
    if (IQ.Type.set == geoIQ.getType() && geoIQ.getTo().toString().startsWith("pubsub")) {
        // find 'geoloc'
        Element element = geoIQ.getChildElement();
        Element action = element.element("publish");
        if (action != null) {
            String nodeID = action.attributeValue("node");
            if (nodeID != null && nodeID.endsWith(TopicHelper.TOPIC_GEOLOC)) {
                // Entity publishes an item
                geoMessage = new Message();
                geoMessage.setType(Message.Type.chat);
                Map<String, String> geoValues = new HashMap<String, String>();
                JID from = geoIQ.getFrom();
                String appId = JIDUtil.getAppId(from);
                geoValues.put(APP_ID, appId);

                String userId = JIDUtil.getUserId(from);
                geoValues.put(USER_ID, userId);

                String deviceId = JIDUtil.getResource(from.toString());
                geoValues.put(DEVICE_ID, deviceId);

                Iterator<Element> items = action.elementIterator("item");
                if (items != null) {
                    while (items.hasNext()) {
                        Element item = (Element) items.next();
                        Element mmx = item.element(Constants.MMX_ELEMENT);
                        if (mmx == null)
                            continue;
                        Element payload = mmx.element(Constants.MMX_PAYLOAD);
                        if (payload == null)
                            return null; // not a valid MMX payload, ignore it
                        Attribute timestamp = payload.attribute(Constants.MMX_ATTR_STAMP);
                        if (timestamp != null) {
                            geoValues.put(Constants.MMX_ATTR_STAMP, timestamp.getValue());
                        }
                        String geoData = payload.getTextTrim();
                        GeoLoc geoLoc = GsonData.getGson().fromJson(geoData, GeoLoc.class);
                        if (geoLoc.getAccuracy() != null) {
                            geoValues.put(ACCURACY, geoLoc.getAccuracy().toString());
                        }
                        if (geoLoc.getLng() != null && geoLoc.getLat() != null) {
                            geoValues.put(LONG, geoLoc.getLng().toString());
                            geoValues.put(LAT, geoLoc.getLat().toString());
                            // calculate geohash
                            GeoPoint point = new GeoPointDefaultImpl(geoLoc.getLat(), geoLoc.getLng());
                            geoValues.put(GEOHASH, localGeoEndocoder.get().encodePoint(point));
                        }
                        if (geoLoc.getAlt() != null) {
                            geoValues.put(ALTITUDE, Integer.toString((int) geoLoc.getAlt().floatValue()));
                        }
                        Element geoElement = geoMessage.addChildElement(Constants.MMX_ELEMENT,
                                Constants.MMX_NS_CONTEXT);
                        geoElement.addAttribute(Constants.MMX_ATTR_MTYPE, Constants.MMX_MTYPE_GEOLOC);
                        JSONObject geoJson = new JSONObject(geoValues);
                        geoElement.setText(geoJson.toString());
                        geoMessage.setBody(geoJson.toString());

                        return geoMessage;

                    }
                }
            }
        }
    }
    return null;
}