Example usage for org.dom4j Element addElement

List of usage examples for org.dom4j Element addElement

Introduction

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

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

From source file:BlastResultXMLsplit.BlastXMLsplit.java

public BlastXMLsplit(String filepath, int seqnumber)
        throws FileNotFoundException, IOException, ParserConfigurationException {
    SAXReader reader = new SAXReader();
    reader.setValidation(false);/*from   w w w .j av 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:bookmarks.BookmarkXML.java

License:Open Source License

/**
 * Adds a Target element to the XML for use by XSL templates
 * //from ww w.  ja v a 2 s . c o  m
 * @param target
 */
@SuppressWarnings("unused")
public void addXSLTarget(String target) {
    Element root = (Element) xmlDoc.getRootElement();

    if (root == null) {
        root = xmlDoc.addElement(XML);
    }

    if (target == null) {
        target = new String();
    }
    Element targetXml = root.addElement("Target").addText(target);
}

From source file:bookmarks.BookmarkXML.java

License:Open Source License

/**
 * /*from ww  w  .j a v  a2 s  .  c om*/
 * @param userName
 * @param resultMsg
 */
@SuppressWarnings("unused")
public void constructWithResultMsg(String userName, String resultMsg) {
    Element root = xmlDoc.addElement(XML);
    if (!userName.equals("SessionDummy") && !userName.equals("SessionDummyChanged")) {
        Element loggedInAs = root.addElement("LoggedInAs").addText(userName);
    }
    if (resultMsg == null) {
        resultMsg = new String();
    }

    Element resultXml = root.addElement("Result").addText(resultMsg);

}

From source file:bookmarks.BookmarkXML.java

License:Open Source License

/**
 * /*from  w ww .ja  v  a  2s . com*/
 * @param userName
 * @param resultMsgs
 */
@SuppressWarnings("unused")
public void constructWithMultResultMsg(String userName, ArrayList<ResultMessage> resultMsgs,
        String newClientMail, String newPassword, String newPassword2, String oldPassword) {
    Element root = xmlDoc.addElement(XML);
    if (!userName.equals("SessionDummy") && !userName.equals("SessionDummyChanged")) {
        Element loggedInAs = root.addElement("LoggedInAs").addText(userName);
    }
    if (resultMsgs == null) {
        resultMsgs.add(new ResultMessage("", ""));
    }

    Element resultXml = root.addElement("Result");

    for (Iterator it = resultMsgs.iterator(); it.hasNext();) {
        ResultMessage resultMsg = (ResultMessage) it.next();
        Element resultXmlMsg = resultXml.addElement("Message");
        Element resultXmlMsgVal = resultXmlMsg.addElement("Value").addText(resultMsg.getMessage());

        Element resultXmlMsgType = resultXmlMsg.addElement("Type").addText(resultMsg.getType());

    }

    Element name = root.addElement("NewClientMail").addText(newClientMail);

    Element pass = root.addElement("NewPassword").addText(newPassword);

    Element pass2 = root.addElement("NewPassword2").addText(newPassword2);

    Element mail = root.addElement("OldPassword").addText(oldPassword);
}

From source file:bookmarks.BookmarkXML.java

License:Open Source License

/**
 * /*ww w.j a  v a  2s.  c o  m*/
 * @param userName
 * @param error
 */
@SuppressWarnings("unused")
public void constructAddClientResult(String userName, String error, String clientName, String clientMail) {
    Element root = xmlDoc.addElement(XML);
    if (!userName.equals("SessionDummy") && !userName.equals("SessionDummyChanged")) {
        Element loggedInAs = root.addElement("LoggedInAs").addText(userName);

        if (error == null) {
            error = new String();
        }

        if (clientName == null) {
            clientName = new String();
        }

        if (clientMail == null) {
            clientMail = new String();
        }

        Element errorXml = root.addElement("Result").addText(error);

        Element name = root.addElement("ClientName").addText(clientName);

        Element mail = root.addElement("ClientMail").addText(clientMail);
    }
}

From source file:bookmarks.BookmarkXML.java

License:Open Source License

/**
 * //from   w  w w. j a  v a2  s.c om
 * @param userName
 * @param url
 */
@SuppressWarnings("unused")
public void constructAddLink(String userName, String url) {
    Element root = xmlDoc.addElement(XML);
    if (!userName.equals("SessionDummy") && !userName.equals("SessionDummyChanged")) {
        Element loggedInAs = root.addElement("LoggedInAs").addText(userName);
    }
    if (url == null) {
        url = new String("");
    }

    Element urlE = root.addElement("URL").addText(url);
}

From source file:bookmarks.BookmarkXML.java

License:Open Source License

/**
 * //from   w w  w  .j  a  va2  s.  c o m
 * @param userName
 * @param linkName
 * @param linkURL
 * @param linkPermission
 * @param linkDescription
 * @param linkLabels
 * @param toolbarPosition
 * @param error
 */
@SuppressWarnings("unused")
public void constructAddLinkResult(String userName, String linkName, String linkURL, String linkPermission,
        String linkDescription, String linkLabels, String toolbarPosition, String error) {
    Element root = xmlDoc.addElement(XML);
    if (!userName.equals("SessionDummy") && !userName.equals("SessionDummyChanged")) {
        Element theLinkName = root.addElement("Name").addText(linkName);

        Element theLinkURL = root.addElement("URL").addText(linkURL);

        Element theLinkPermission = root.addElement("Permission").addText(linkPermission);

        Element theLinkDescription = root.addElement("Description").addText(linkDescription);

        Element theLinkLabels = root.addElement("Labels").addText(linkLabels);

        Element loggedInAs = root.addElement("LoggedInAs").addText(userName);

        Element errorXml = root.addElement("Result").addText(error);

        Element theToolbarPosition = root.addElement("ToolbarPosition").addText(toolbarPosition);
    }
}

From source file:bookmarks.BookmarkXML.java

License:Open Source License

/**
 * /*from www. j  a  va  2 s . com*/
 * @param userName
 * @param error
 */
@SuppressWarnings("unused")
public void constructAddLinkResult(String userName, String error) {
    Element root = xmlDoc.addElement(XML);
    if (!userName.equals("SessionDummy") && !userName.equals("SessionDummyChanged")) {
        Element loggedInAs = root.addElement("LoggedInAs").addText(userName);

        Element errorXml = root.addElement("Result").addText(error);
    }
}

From source file:bookmarks.BookmarkXML.java

License:Open Source License

/**
 * /* w w w . j a v a 2s .  com*/
 * @param userName
 * @param target
 * @param error
 */
@SuppressWarnings("unused")
public void constructImportResult(String userName, String target, String error) {
    if (!userName.equals("SessionDummy") && !userName.equals("SessionDummyChanged")) {
        Element root = xmlDoc.addElement(XML);

        Element loggedInAs = root.addElement("LoggedInAs").addText(userName);

        Element errorXml = root.addElement("Result").addText(error);

        Element targetXml = root.addElement("Target").addText(target);
    }
}

From source file:bookmarks.BookmarkXML.java

License:Open Source License

/**
 * /*from  ww w.  ja  v  a2  s. c om*/
 * @param userName
 * @param bookmark
 */
public void constructRandom(String userName, LinkNameUrlObject random) {
    Element root = xmlDoc.addElement(XML);
    root.addElement("LoggedInAs").addText(userName);
    root.addElement("Result").addText("SUCCESS");
    Element bookmarkList = root.addElement("BookmarkList");
    Element bookmark = bookmarkList.addElement("Bookmark");
    bookmark.addElement("Title").addText(random.getLinkName());
    bookmark.addElement("URL").addText(random.getLinkURL());
    bookmark.addElement("EncodedLink").addText(Utilities.encodeURIComponent(random.getLinkURL()));
    bookmark.addElement("EncodedName").addText(Utilities.encodeURIComponent(random.getLinkName()));
    if (!userName.equals("")) {
        bookmark.addElement("LoggedIn");
    }
}