当前位置: 动力学知识库 > 问答 > 编程问答 >

How to insert a xml node as first child in another xml document in java using dom4j?

问题描述:

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>

网友答案:

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) 
网友答案:

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

网友答案:

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>`
分享给朋友:
您可能感兴趣的文章:
随机阅读: