0

I need to create multiple Child Nodes in one element node in XML, do I just append as many times as required to create these nodes? Like this:

rootElement.appendChild(creator);
creator.appendChild(name);
creator.appendChild(email);
creator.appendChild(name);
creator.appendChild(email);

Or does java automatically create the extra child nodes whenever I do this:

name.appendChild(doc.createTextNode("Bob"));
email.appendChild(doc.createTextNode("[email protected]"));
name.appendChild(doc.createTextNode("Smith"));
email.appendChild(doc.createTextNode("[email protected]"));

I'm not too sure how it works, any advice or help would be appreciated!

1 Answer 1

1

Behavior varies across different implementations, but in general you want to go with the second approach.

When appending or adding a child to a parent the previous parent is replaced. This means that the first approach does nothing but shuffle the same to children. The second approach is correct because you create new children as you go and the previously added children remain untouched by later API calls.

Sign up to request clarification or add additional context in comments.

1 Comment

do you know how to write the XML file to a specified folder?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.