Example usage for org.dom4j Element remove

List of usage examples for org.dom4j Element remove

Introduction

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

Prototype

boolean remove(Text text);

Source Link

Document

Removes the given Text if the node is an immediate child of this element.

Usage

From source file:InitializeDB.java

License:Open Source License

public static void clearEntryWithVulsoft(String filename) {

    try {/*from   w w w  .jav  a 2 s  .  c o m*/

        SAXReader saxReader = new SAXReader();

        Document document = saxReader.read(filename);

        List soft = document

                .selectNodes("/*[local-name(.)='nvd']/*[local-name(.)='entry']/*[local-name(.)='vuln_soft']");

        Iterator sft = soft.iterator();

        Element nvd = (Element) document

                .selectSingleNode("/*[local-name(.)='nvd']");

        while (sft.hasNext()) {

            Element vsft = (Element) sft.next();

            nvd.remove(vsft.getParent());

            XMLWriter output = new XMLWriter(new FileWriter(filename));//

            output.write(document);

            output.flush();

            output.close();

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

}

From source file:architecture.common.lifecycle.internal.XmlApplicationPropertiesOld.java

License:Apache License

public synchronized String remove(Object key) {
    String name = (String) key;
    propertyCache.remove(name);//from w w w  .j  a v  a 2s  . c o m
    String propName[] = parsePropertyName(name);
    Element element = doc.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        element = element.element(propName[i]);
        if (element == null)
            return null;
    }

    String value = element.getText();
    element.remove(element.element(propName[propName.length - 1]));
    saveProperties();
    return value;
}

From source file:architecture.common.xml.XmlProperties.java

License:Apache 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://from  w w w.  j a  va2  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>
 *
 * @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:architecture.common.xml.XmlProperties.java

License:Apache License

/**
 * Sets the value of the specified property. If the property doesn't
 * currently exist, it will be automatically created.
 *
 * @param name/*from  w w w  .j a  va  2  s.co  m*/
 *            the name of the property to set.
 * @param value
 *            the new value for the property.
 */
public synchronized void setProperty(String name, String value) {
    if (!StringEscapeUtils.escapeXml(name).equals(name)) {
        throw new IllegalArgumentException(L10NUtils.getMessage("002155"));
    }
    if (name == null) {
        return;
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(aPropName) == null) {
            element.addElement(aPropName);
        }
        element = element.element(aPropName);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        Iterator it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = (Node) it.next();
            if (node instanceof CDATA) {
                element.remove(node);
                break;
            }
        }
        element.addCDATA(value.substring(9, value.length() - 3));
    } else {
        element.setText(value);
    }
    // Write the XML properties to disk
    saveProperties();

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

    // PropertyEventDispatcher.dispatchEvent(name,
    // PropertyEventDispatcher.EventType.xml_property_set, params);
}

From source file:architecture.common.xml.XmlProperties.java

License:Apache License

/**
 * Deletes the specified property./*from  w  ww  .j a  v  a2  s .  c o  m*/
 *
 * @param name
 *            the property to delete.
 */
public synchronized void deleteProperty(String name) {
    // Remove property from cache.
    propertyCache.remove(name);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        element = element.element(propName[i]);
        // Can't find the property so return.
        if (element == null) {
            return;
        }
    }
    // Found the correct element to remove, so remove it...
    element.remove(element.element(propName[propName.length - 1]));
    // .. then write to disk.
    saveProperties();

    // Generate event.
    Map<String, Object> params = Collections.emptyMap();

    // PropertyEventDispatcher.dispatchEvent(name,
    // PropertyEventDispatcher.EventType.xml_property_deleted, params);
}

From source file:architecture.ee.util.xml.XmlProperties.java

License:Apache 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://from w  w w  .ja  v  a 2s .  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>
 *
 * @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(XmlEscapers.xmlContentEscaper().escape(value));// 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:architecture.ee.util.xml.XmlProperties.java

License:Apache License

/**
 * Sets the value of the specified property. If the property doesn't
 * currently exist, it will be automatically created.
 *
 * @param name//from w  ww .j av a 2  s .  c  o m
 *            the name of the property to set.
 * @param value
 *            the new value for the property.
 */
public synchronized void setProperty(String name, String value) {

    if (!XmlEscapers.xmlContentEscaper().escape(name).equals(name)) {
        throw new IllegalArgumentException();// L10NUtils.getMessage("002155"));
    }
    if (name == null) {
        return;
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(aPropName) == null) {
            element.addElement(aPropName);
        }
        element = element.element(aPropName);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        Iterator it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = (Node) it.next();
            if (node instanceof CDATA) {
                element.remove(node);
                break;
            }
        }
        element.addCDATA(value.substring(9, value.length() - 3));
    } else {
        element.setText(value);
    }
    // Write the XML properties to disk
    saveProperties();

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

    // PropertyEventDispatcher.dispatchEvent(name,
    // PropertyEventDispatcher.EventType.xml_property_set, params);
}

From source file:BlastResultXMLsplit.BlastXMLsplit.java

public BlastXMLsplit(String filepath, int seqnumber)
        throws FileNotFoundException, IOException, ParserConfigurationException {
    SAXReader reader = new SAXReader();
    reader.setValidation(false);//  w  w w.ja  v a  2  s.c  o  m
    try {
        System.out.println("Xlmfile reading");
        Document document = reader.read(new FileInputStream(filepath));//XMLdocument?
        System.out.println("Xlmfile read done!");
        org.dom4j.DocumentFactory DocumentFactory = new org.dom4j.DocumentFactory();

        ArrayList str = new ArrayList();//??? 

        String fileoutpath;

        org.dom4j.Element rootElm = document.getRootElement();//
        File f = new File(filepath);
        int count = 1;
        List<org.dom4j.Element> blastOutput_iterations = rootElm.element("BlastOutput_iterations").elements();//?
        org.dom4j.Element BlastOutput_program = rootElm.element("BlastOutput_program");
        org.dom4j.Element BlastOutput_version = rootElm.element("BlastOutput_version");
        org.dom4j.Element BlastOutput_reference = rootElm.element("BlastOutput_reference");
        org.dom4j.Element BlastOutput_db = rootElm.element("BlastOutput_db");
        //?
        BlastOutput_program.getParent().remove(BlastOutput_program);
        BlastOutput_version.getParent().remove(BlastOutput_version);
        BlastOutput_reference.getParent().remove(BlastOutput_reference);
        BlastOutput_db.getParent().remove(BlastOutput_db);
        // org.dom4j.Element BlastOutput_query_def=rootElm.element("BlastOutput_query-def");
        //org.dom4j.Element BBlastOutput_query_len=rootElm.element("BlastOutput_query-len");

        org.dom4j.Element BlastOutput_param = rootElm.element("BlastOutput_param");
        rootElm.remove(BlastOutput_param);

        String Iteration_queryIDstr, Iteration_queryDefstr, Iteration_query_len;
        List<org.dom4j.Element> Iterationlist = null;
        int size = blastOutput_iterations.size();
        System.out.println("Your query seqcount is " + size + "\r\n Start dividing your file");
        for (int i = 0; i < size; i = i + seqnumber) {
            //??
            fileoutpath = f.getParent() + System.getProperty("file.separator") + count + ".xml";
            count++;
            System.out.println("The " + count + " is located in " + fileoutpath);
            FileOutputStream fos = new FileOutputStream(fileoutpath);
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(fos, format);
            // XMLWriter writer = new XMLWriter(new FileOutputStream(fileoutpath));
            //w??
            org.dom4j.Element firstelement = blastOutput_iterations.get(i);
            Iteration_queryIDstr = firstelement.element("Iteration_query-ID").getText();
            Iteration_queryDefstr = firstelement.element("Iteration_query-def").getText();
            Iteration_query_len = firstelement.element("Iteration_query-len").getText();

            //org.dom4j.DocumentFactory DocumentFactory = new org.dom4j.DocumentFactory();

            Document document2 = DocumentHelper.createDocument();
            ;
            org.dom4j.Element BlastOutputElement = document2.addElement("BlastOutput");
            //doc.setRootElement(BlastOutputElement);
            //;
            BlastOutputElement.add(BlastOutput_program);
            BlastOutputElement.add(BlastOutput_version);
            BlastOutputElement.add(BlastOutput_reference);
            BlastOutputElement.add(BlastOutput_db);
            BlastOutputElement.addElement("BlastOutput_query-ID");
            BlastOutputElement.element("BlastOutput_query-ID").setText(Iteration_queryIDstr);
            BlastOutputElement.addElement("BlastOutput_query-def");
            BlastOutputElement.element("BlastOutput_query-def").setText(Iteration_queryDefstr);
            BlastOutputElement.addElement("BlastOutput_query-len");
            BlastOutputElement.element("BlastOutput_query-len").setText(Iteration_query_len);
            //                Element BlastOutput_param_new=DocumentFactory.createElement("BlastOutput_param");
            //                for (Iterator it = BlastOutput_param.elementIterator(); it.hasNext();) {
            //                    Element tempele=(Element) it.next();
            //                    tempele.getParent().remove(tempele);
            //                    BlastOutput_param_new.add(tempele);
            //                }
            BlastOutputElement.add(BlastOutput_param);
            //BlastOutputElement.add(BlastOutput_param);
            if (i + seqnumber < blastOutput_iterations.size()) {
                Iterationlist = blastOutput_iterations.subList(i, i + seqnumber);
            } else {
                Iterationlist = blastOutput_iterations.subList(i, blastOutput_iterations.size() - 1);
            }
            //System.out.println(Iterationlist.size());
            //?query
            resetIterationlist(Iterationlist);
            //?BlastOutput_iterations

            Element BlastOutput_iterations = DocumentFactory.createElement("BlastOutput_iterations");
            //BlastOutputElement.addAttribute("BlastOutput_iterations");
            //org.dom4j.Element BlastOutput_iterations = new org.dom4j.Element("BlastOutput_iterations");
            for (int j = 0; j < Iterationlist.size(); j++) {
                Iterationlist.get(j).getParent().remove(Iterationlist.get(j));
                //System.out.println(j);
                BlastOutput_iterations.add(Iterationlist.get(j));
            }
            BlastOutputElement.add(BlastOutput_iterations);

            //
            writer.write(document2);
            writer.close();
            BlastOutput_program.getParent().remove(BlastOutput_program);
            BlastOutput_version.getParent().remove(BlastOutput_version);
            BlastOutput_reference.getParent().remove(BlastOutput_reference);
            BlastOutput_db.getParent().remove(BlastOutput_db);
            BlastOutput_param.getParent().remove(BlastOutput_param);
        }
    } catch (DocumentException ex) {
        Logger.getLogger(BlastXMLsplit.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:cn.com.iscs.base.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://  www  .j  a v  a2  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>
 * 
 * @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:cn.com.iscs.base.util.XMLProperties.java

License:Open Source License

/**
 * Sets the value of the specified property. If the property doesn't currently
 * exist, it will be automatically created.
 * /*from  w  w  w.j a  v a 2  s  . c  om*/
 * @param name
 *          the name of the property to set.
 * @param value
 *          the new value for the property.
 */
public synchronized void setProperty(String name, String value) {
    if (!StringEscapeUtils.escapeXml(name).equals(name)) {
        throw new IllegalArgumentException("Property name cannot contain XML entities.");
    }
    if (name == null) {
        return;
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(aPropName) == null) {
            element.addElement(aPropName);
        }
        element = element.element(aPropName);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        Iterator it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = (Node) it.next();
            if (node instanceof CDATA) {
                element.remove(node);
                break;
            }
        }
        element.addCDATA(value.substring(9, value.length() - 3));
    } else {
        element.setText(value);
    }
    // Write the XML properties to disk
    saveProperties();

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