dmurali
dmurali

Reputation: 293

Inserting a node in an xml file

I would like to insert a node in an xml file using Java DOM. I am actually editing a lot of contents of a dummy file in order to mofidy it like the original.

I would want to add an open node and close node in between the following file;

            <?xml version="1.0" encoding="utf-8"?>
            <Memory xmlns:xyz="http://www.w3.org/2001/XMLSchema-instance"   
            xmlns:abc="http://www.w3.org/2001/XMLSchema" Derivative="ABC"            
            xmlns="http://..">

       ///////////<Address> ///////////(which I would like to insert)

            <Block ---------
            --------
            -------
            />

      ////////// </Address> /////////(which I would like to insert)

            <Parameters Thread ="yyyy" />
            </Memory>

I hereby request you to let me know how to I insert -- in between the xml file?

Thanks in advance.!

What I have tried doing is;

            Element child = doc.createElement("Address");
    child.appendChild(doc.createTextNode("Block"));
    root.appendChild(child);

But this gives me an output like;

        <Address> Block </Address> and not the way i expect :(

And now, what I have tried is to add these lines;

            Element cd = doc.createElement("Address");
            Node Block = root.getFirstChild().getNextSibling();
        cd.appendChild(Block);
        root.insertBefore(cd, root.getFirstChild());

But still, this is not the output which i am looking for. I got this output as ---------

Upvotes: 0

Views: 3532

Answers (3)

forty-two
forty-two

Reputation: 12817

Here:

DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = b.parse(...);

// Parent of existing Block elements and new Address elemet
// Might be retrieved differently depending on 
// actual structure
Element parent = document.getDocumentElement();
Element address = document.createElement("address");

NodeList nl = parent.getElementsByTagName("Block");
for (int i = 0; i < nl.getLength(); ++i) {
    Element block = (Element) nl.item(i);
    if (i == 0)
        parent.insertBefore(address, block);
    parent.removeChild(block);
    address.appendChild(block);
}

// UPDATE: how to pretty print

LSSerializer serializer = 
    ((DOMImplementationLS)document.getImplementation()).createLSSerializer();
serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
LSOutput output = 
    ((DOMImplementationLS)document.getImplementation()).createLSOutput();
output.setByteStream(System.out);
serializer.write(document, output);

Upvotes: 1

billc.cn
billc.cn

Reputation: 7317

What you want is probably:

Node parent = block.getParentNode()
Node blockRemoved = parent.removeChild(block);
// Create address
parent.appendChild(address);
address.appendChild(blockRemoved);

This is the standard way to re-attach a node in another place under W3C DOM.

Upvotes: 2

peter.murray.rust
peter.murray.rust

Reputation: 38043

I assume you are using the W3C DOM (e.g. http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html ). If so try

insertBefore(address, block);

Upvotes: 0

Related Questions