For example,I want to generate the following xml file by using java with DOM
<catalogue>
<books>
<book id="1">
<name>Gone with the wind</name>
<quantity>2</quantity>
</book>
<book id="2">
<name>Call of the wind</name>
<quantity>3</quantity>
</book>
<book id="3">
<quality>Good</quality>
</book>
</books>
</catalogue>
It’s not very difficult to produce xml file with only 1 node named book, but with more than 1 with the same name, I dont know how to do it? I got the error:
Duplicate local variable
This is one part of my java code:
I tried to create the first book element with the code:
Element book = doc.createElement("book");
rootElement.appendChild(book);
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode("Gone with the wind"));
book.appendChild(name);
And then I used the same code to create the second and the third book element, and I found the error.
Is there any other way to do it?
Can anyone give me a suggestion please?
Thank you very much for your time
I’m guessing you are appending the same object twice. You need to call createElement each time.
This won’t work
You need to do
Complete example