2

There is a similar post using w3c dom using insertBefore() in a . I was wondering how do this using dom4j. I want to insert <div id="dynamicdiv"/> as the first element of body. <html><head/><body>[<div id="dynamicdiv">] <many tags></body></html>

3 Answers 3

2

Thanks Mat Banik I managed to get your alternative approach to work, I am posting this for others to get benefited. You can get the advantage of both dom4j and w3c dom. Moreover there is only one tree constructed internally which makes the print(document.asXML()) to incorporate the manipulations done by w3c.

Listing

package playground;

import java.io.StringReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.dom.DOMDocument;
import org.dom4j.dom.DOMDocumentFactory;
import org.dom4j.io.SAXReader;

public class Dom4jInsertBefore {
public static void main(String[] args) throws DocumentException {
    String newNode = "<node>value</node>"; // Convert this to XML
    String text = "<root><given></given></root>";
    // Document document = DocumentHelper.parseText(text); //type casting
    // exception will come while converting to DOMDocument 
    // use DOMDocumentFactory
     
    
    // Document newNodeDocument = DocumentHelper.parseText(newNode);
    
    DOMDocumentFactory factory = new DOMDocumentFactory();
    SAXReader reader2 = new SAXReader();
    reader2.setDocumentFactory(factory);
    org.dom4j.Document document = reader2.read(new StringReader(text));
    Document newNodeDocument = reader2.read(new StringReader(newNode));

    Element givenNode = document.getRootElement().element("given");
    givenNode.add(newNodeDocument.getRootElement());

    org.dom4j.dom.DOMDocument w3cDoc = (DOMDocument) document;
    org.w3c.dom.Element e = w3cDoc.createElement("div");
    e.setAttribute("id", "someattr");

    w3cDoc.getDocumentElement().getFirstChild().insertBefore(e, 
w3cDoc.getDocumentElement().getElementsByTagName("node").item(0));
    // w3cDoc.getDocumentElement().getFirstChild().appendChild(e); this works

    System.out.println(document.asXML());
}
}

Output

<?xml version="1.0" encoding="UTF-8"?>
<root><given><div id="someattr"/><node>value</node></given></root>`
Sign up to request clarification or add additional context in comments.

Comments

1

dom4j supports DOM api as well. Look here for the same method.

Comments

0

I wrote this on the fly without testing but I think this should get you started:

import org.jdom.Attribute;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.Document;
import org.jdom.Element;

        Document document = new Document();
        Element html = new Element("html");            
        Element body = new Element("body");
        Element head = new Element("head");

        Element div = new Element("div");
        Attribute id = new Attribute("id", "dynamicdiv");
        Element moreElements = new Element("moreElements");

        document.setRootElement(html);

        div.addContent("");
        div.setAttribute(id)
        moreElements.addContent("");

        body.addContent(div);
        body.addContent(moreElements);
        html.addContent(head);
        html.addContent(body);

Or alternatively you could use this method:

Node org.dom4j.dom.DOMElement.insertBefore(Node newChild, Node refChild) 

3 Comments

Thanks for the alternative method you described which said DOMElement has insertBefore. But this is actually org.dom4j.dom.DOMElement.insertBefore(org.w3c.dom.Node newNode2,org.w3c.dom.Node refNode ); which is again w3c dom. I believe conversion from dom4j dom to w3c dom requires serialization which is quite expensive operation.
Man you changed my life! I was thinking of using html cleaner, which has pretty good javascript like selectors (insertBefore is missing), but what more can I ask for if there is jquery like selectors and manipulators. If this is faster than xom, dom4j, or w3c dom I will use it for XML parsing as well, and i tested it does handle <!CDATA[]]> as well, looks promising.
I got the type casting of org.dom4j.Document into DOMDocument working. DOMDocumentFactory factory = new DOMDocumentFactory(); SAXReader reader2 = new SAXReader(); reader2.setDocumentFactory(factory); //Document document = DocumentHelper.parseText(text); **Type casting exception will come when converting to DOMDocument** [see post here](http://www.mail-archive.com/[email protected]/msg01945.html) org.dom4j.Document document = reader2.read(new StringReader(text)); org.dom4j.dom.DOMDocument w3cDoc = (DOMDocument) document; Good thing is only one tree is created internally

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.