Reputation: 1315
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>
Upvotes: 2
Views: 5339
Reputation: 1315
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.
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());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<root><given><div id="someattr"/><node>value</node></given></root>`
Upvotes: 2
Reputation: 26860
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)
Upvotes: 0
Reputation: 80176
dom4j supports DOM api as well. Look here for the same method.
Upvotes: 1