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.suneee.core.util.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>/* www.  j a  v a2  s  . com*/
 * &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) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // 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 heirarchy
        // 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 iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) 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 it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            childElement.setText(StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    /*Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);
    PropertyEventDispatcher.dispatchEvent(name,
        PropertyEventDispatcher.EventType.xml_property_set, params);*/
}

From source file:com.suneee.core.util.XMLProperties.java

License:Open Source License

public void setPropertiesList(String name, List<Map<String, String>> list) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // 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 heirarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }/*from  w w w . j a v  a 2  s  .  c  o  m*/
        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 iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (int j = 0; j < list.size(); j++) {
        Element childElement = element.addElement(childName);
        Map<String, String> map = (Map<String, String>) list.get(j);
        for (Map.Entry<String, String> record : map.entrySet()) {
            Element e = childElement.addElement(record.getKey());
            e.setText(record.getValue());
            //            setText(StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    /*Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);
    PropertyEventDispatcher.dispatchEvent(name,
        PropertyEventDispatcher.EventType.xml_property_set, params);*/
}

From source file:com.tao.realweb.util.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>/*from   ww  w . j a  va  2 s . com*/
 * &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) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // 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 heirarchy
        // 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 iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) 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 it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            childElement.setText(StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);
    PropertyEventDispatcher.dispatchEvent(name, PropertyEventDispatcher.EventType.xml_property_set, params);
}

From source file:com.uletian.ultcrm.business.service.CustomerInfoMessageService.java

public void handlerCustomerInfo(String message) throws SAXException, DocumentException, IOException {
    StringWriter writer = new StringWriter();
    OutputFormat format = OutputFormat.createPrettyPrint();
    Document doc;//from   w ww . ja  va2 s  . co m
    doc = DocumentHelper.parseText(message);
    XMLWriter xmlwriter = new XMLWriter(writer, format);
    xmlwriter.write(doc);
    logger.info("??\n" + writer.toString());

    Element element = doc.getRootElement();
    String action = element.elementText("action");
    String sourceSys = element.elementText("sourceSys");
    String ultcrmid = element.elementText("ultcrmid");
    String crmid = element.elementText("crmid");
    String name = element.elementText("name");
    String sexy = element.elementText("sexy");
    String telephone = element.elementText("telephone");
    String country = element.elementText("country");
    String province = element.elementText("province");
    String city = element.elementText("city");
    String address = element.elementText("address");
    String postcode = element.elementText("postcode");

    if (!"UPDATE".equals(action)) {
        logger.debug("?\ncrmid:" + crmid + "\nultcrmid:" + ultcrmid);
        return;
    }
    Customer customer = customerRepository.findOne(Long.decode(ultcrmid));
    if (customer == null) {
        logger.warn("?" + ultcrmid);
        return;
    }
    customer.setPhone(telephone);
    customer.setSyncid(crmid);
    customer.setName(name);
    customer.setSex(sexy);
    customer.setCountry(country);
    customer.setProvince(province);
    customer.setCity(city);
    customer.setAddress(address);
    customer.setPostcode(postcode);
    customer.setCrmCustomerId(crmid);
    customerRepository.save(customer);

    Element elements = element.element("techs");
    Iterator<Element> iterator = elements.elementIterator("tech");
    ArrayList<Tech> techs = new ArrayList<Tech>(0);

    while (iterator.hasNext()) {
        Element techElement = iterator.next();
        String code = techElement.elementText("code");
        String techlevelno = techElement.elementText("techlevelno");
        String techerno = techElement.elementText("techerno");
        String techname = techElement.elementText("techname");
        String coursetime = techElement.elementText("coursetime");
        String trainExpireDate = techElement.elementText("trainExpireDate");
        String trainCompany = techElement.elementText("trainCompany");
        String crmTechId = techElement.elementText("crmtechid");
        String courseCode = techElement.elementText("courseCode");
        String techColor = techElement.elementText("techColor");
        String registerDate = techElement.elementText("registerDate");
        String courseLicense = techElement.elementText("courseLicense");
        String checkExpireDate = techElement.elementText("checkExpireDate");
        String memberLevel = techElement.elementText("memberLevel");

        String nextMaintCoursetime = techElement.elementText("nextMaintCoursetime");
        String nextMaintDate = techElement.elementText("nextMaintDate");
        String lastConsumeDate = techElement.elementText("lastConsumeDate");

        Tech tech = techRepository.findTechByTechlevelno(techlevelno);
        if (tech == null) {
            tech = new Tech();
        }
        TechModel techModel = null;
        try {
            techModel = techModelRepository.findModelByCode(code);
        } catch (Exception e) {
        }
        if (techModel == null) {
            logger.warn("??\n" + code);
            techModel = techModelRepository.findModelByCode("OTHERS");
        }
        tech.setCrmTechId(crmTechId);
        tech.setTechlevelno(techlevelno);
        tech.setTechModel(techModel);
        tech.setTechSery(techModel.getTechSery());
        tech.setTechCourse(techModel.getTechSery().getTechCourse());
        tech.setCustomer(customer);
        tech.setCode(code);
        tech.setTechlevelno(techlevelno);
        tech.setTecherno(techerno);
        tech.setTechname(techname);
        tech.setCoursetime(coursetime);

        try {
            tech.setTrainExpireDate(sdf.parse(trainExpireDate));
        } catch (ParseException e) {
            logger.warn("???\n" + trainExpireDate, e);
        }
        tech.setTrainCompany(trainCompany);
        tech.setCourseCode(courseCode);
        tech.setMemberLevel(memberLevel);
        tech.setColor(techColor);
        try {
            tech.setRegisterDate(sdf.parse(registerDate));
        } catch (ParseException e) {
            logger.warn("??\n" + registerDate, e);
        }
        tech.setCourseLicense(courseLicense);
        try {
            tech.setCheckExpireDate(sdf.parse(checkExpireDate));
        } catch (ParseException e) {
            logger.warn("??\n" + checkExpireDate, e);
        }

        // ?by xiecheng 2015-11-19
        tech.setNextMaintCoursetime(nextMaintCoursetime);
        try {
            tech.setNextMaintDate(sdf.parse(nextMaintDate));
        } catch (ParseException e) {
            logger.warn("??\n" + nextMaintDate, e);
        }

        try {
            tech.setLastConsumeDate(sdf.parse(lastConsumeDate));
        } catch (ParseException e) {
            logger.warn("??\n" + lastConsumeDate, e);
        }

        techs.add(tech);
    }
    techRepository.save(techs);
}

From source file:com.weibo.wesync.notify.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>//from   w  w  w. ja  va 2  s . co  m
 * &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) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // 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 heirarchy
        // 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 iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) 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 it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            childElement.setText(StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);
}

From source file:com.zimbra.common.localconfig.LocalConfig.java

License:Open Source License

public LocalConfig(String file) throws DocumentException, ConfigException {
    mConfigFile = file;/*w  ww  .  j  av a  2s. c  om*/
    if (mConfigFile == null) {
        mConfigFile = defaultConfigFile();
    }

    File cf = new File(mConfigFile);
    if (cf.exists() && cf.canRead()) {
        try (FileInputStream fis = new FileInputStream(cf)) {
            Document document = W3cDomUtil.parseXMLToDom4jDocUsingSecureProcessing(fis);
            Element root = document.getRootElement();

            if (!root.getName().equals(E_LOCALCONFIG))
                throw new DocumentException("config file " + mConfigFile + " root tag is not " + E_LOCALCONFIG);

            for (Iterator<?> iter = root.elementIterator(E_KEY); iter.hasNext();) {
                Element ekey = (Element) iter.next();
                String key = ekey.attributeValue(A_NAME);
                String value = ekey.elementText(E_VALUE);
                set(key, value);
            }
        } catch (IOException | XmlParseException e) {
            Logging.warn(String.format("Problem parsing local config file '%s'", cf), e);
            throw new DocumentException(String.format("Problem parsing local config file '%s'", cf));
        }
    } else {
        Logging.warn("local config file `" + cf + "' is not readable");
    }
    expandAll();
}

From source file:com.zimbra.cs.client.soap.LmcBrowseRequest.java

License:Open Source License

protected LmcSoapResponse parseResponseXML(Element parentElem)
        throws SoapParseException, ServiceException, LmcSoapClientException {
    LmcBrowseResponse response = new LmcBrowseResponse();
    ArrayList bdArray = new ArrayList();
    for (Iterator ait = parentElem.elementIterator(MailConstants.E_BROWSE_DATA); ait.hasNext();) {
        Element a = (Element) ait.next();
        bdArray.add(parseBrowseData(a));
    }//from w  ww  .j  ava  2  s  .c om

    if (!bdArray.isEmpty()) {
        LmcBrowseData bds[] = new LmcBrowseData[bdArray.size()];
        response.setData((LmcBrowseData[]) bdArray.toArray(bds));
    }

    return response;
}

From source file:com.zimbra.cs.client.soap.LmcGetPrefsRequest.java

License:Open Source License

protected LmcSoapResponse parseResponseXML(Element responseXML) throws ServiceException {
    // iterate over all the <pref> elements in the response
    HashMap prefMap = new HashMap();
    for (Iterator ait = responseXML.elementIterator(AccountConstants.E_PREF); ait.hasNext();) {
        Element a = (Element) ait.next();
        addPrefToMultiMap(prefMap, a);//from   www .  j a  v a  2 s. co  m
    }

    // create the response object and put in the HashMap
    LmcGetPrefsResponse response = new LmcGetPrefsResponse();
    response.setPrefsMap(prefMap);
    return response;
}

From source file:com.zimbra.cs.client.soap.LmcGetTagRequest.java

License:Open Source License

protected LmcSoapResponse parseResponseXML(Element responseXML) throws ServiceException {
    // iterate over all the <tag> elements in the response
    ArrayList tags = new ArrayList();
    for (Iterator ait = responseXML.elementIterator(MailConstants.E_TAG); ait.hasNext();) {
        Element a = (Element) ait.next();
        tags.add(parseTag(a));/*from   w ww  .  ja v a2 s  . co m*/
    }

    // create the response object and put in the tags
    LmcGetTagResponse response = new LmcGetTagResponse();
    response.setTags(tags);
    return response;
}

From source file:com.zimbra.cs.client.soap.LmcSoapRequest.java

License:Open Source License

protected LmcContact[] parseContactArray(Element parentElem) throws ServiceException {
    // iterate over all the <cn> elements in parentElem
    ArrayList contactArray = new ArrayList();
    for (Iterator ait = parentElem.elementIterator(MailConstants.E_CONTACT); ait.hasNext();) {
        Element a = (Element) ait.next();
        contactArray.add(parseContact(a));
    }//from  w  w  w.j  a  v  a 2  s.c  o m

    if (contactArray.isEmpty()) {
        return null;
    } else {
        LmcContact contacts[] = new LmcContact[contactArray.size()];
        return (LmcContact[]) contactArray.toArray(contacts);
    }
}