This discussion is archived
5 Replies Latest reply: Sep 8, 2011 11:25 AM by DrClap RSS

[DOM] How to insert a new node ?

886880 Newbie
Currently Being Moderated
Hi guys, I am a bit novice to the subject of XML in Java.
After some time I got to read the data from xml to my Java objects.
Now I would like to make the opposite. Write an xml node from Java Object.

This is my xml :

+<?xml version="1.0" encoding="UTF-8"?>+
+<Base>+
+<coin>+
+<name>Srebro</name>+
+<year>2009</year>+
+<diameter>35</diameter>+
+</coin>+

+<coin>+
+<name>Zloto</name>+
+<year>2010</year>+
+<diameter>40</diameter>+
+</coin>+

+</Base>+
I would like to add the whole part from <coin> to </coin>.

Is it possible ? How to do it?
Thanks in advance

Adam
  • 1. Re: [DOM] How to insert a new node ?
    DrClap Expert
    Currently Being Moderated
    To create a node which you are going to insert into a Document, you call one of the Document's methods whose names are like createElement, createTextNode, and so on.

    Then to put that node into the Document, you decide what its parent node is going to be, and call one of that node's methods whose names are like appendChild and insertBefore.
  • 2. Re: [DOM] How to insert a new node ?
    886880 Newbie
    Currently Being Moderated
    Previously I made something like this :

    Element element = (Element) dom.getElementsByTagName("coin").item(1);
    Element Nelement = dom.createElement("test");
    dom.insertBefore(Nelement, element);

    but I got errors :

    Exception in thread "main" org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
    at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.insertBefore(CoreDocumentImpl.java:391)
    at monetownik.coinDatabase.addRecord(coinDatabase.java:34)
    at monetownik.monetownik.main(monetownik.java:26)
    Java Result: 1

    What shall I do ?
  • 3. Re: [DOM] How to insert a new node ?
    DrClap Expert
    Currently Being Moderated
    Don't insert the node where it isn't permitted, of course.

    That would require you to figure out where you were trying to insert the node and why it wasn't permitted to insert it there. We don't have any context to be able to figure that out.
  • 4. Re: [DOM] How to insert a new node ?
    886880 Newbie
    Currently Being Moderated
    Yes you have a context :)

    <?xml version="1.0" encoding="UTF-8"?>
    <Base>
    <coin>
    <name>Srebro</name>
    <year>2009</year>
    <diameter>35</diameter>
    </coin>

    <coin>
    <name>Zloto</name>
    <year>2010</year>
    <diameter>40</diameter>
    </coin>

    </Base>

    Element element = (Element) dom.getElementsByTagName("coin").item(1); // I look for the second <coin>...</coin>
    Element Nelement = dom.createElement("test"); // I create new element <test>
    dom.insertBefore(Nelement, element); // and then I put it one before another

    So why I have a message that is not permitted ?
  • 5. Re: [DOM] How to insert a new node ?
    DrClap Expert
    Currently Being Moderated
    Read the documentation.
    Inserts the node newChild before the existing child node refChild.
    Here "newChild" is your "test" element and "refChild" is your "coin" element. Which is NOT a child of the document element. It's a child of the "Base" element.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points