davidBecks
davidBecks

Reputation: 3

Generate XML file from java code

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

Upvotes: 0

Views: 8059

Answers (2)

Andrew T Finnell
Andrew T Finnell

Reputation: 13638

I'm guessing you are appending the same object twice. You need to call createElement each time.

This won't work

    Element name = doc.createElement("name");
    name.appendChild(doc.createTextNode("Gone with the wind"));
    book.appendChild(name);

    name.appendChild(doc.createTextNode("Call of the wind"));
    book.appendChild(name);

You need to do

    Element name = doc.createElement("name");
    name.appendChild(doc.createTextNode("Gone with the wind"));
    book.appendChild(name);

    name = doc.createElement("name");
    name.appendChild(doc.createTextNode("Call of the wind"));
    book.appendChild(name);

Complete example

     Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

    Element root = doc.createElement("catalogue");
    doc.appendChild(root);

    Element books = doc.createElement("books");
    root.appendChild(books);

    Element book1 = doc.createElement("book");
    book1.setAttribute("id", "1");
    books.appendChild(book1);

    Element book1_name = doc.createElement("name");
    book1_name.setTextContent("Gone with the wind");
    book1.appendChild(book1_name);

    Element book1_quantity = doc.createElement("quantity");
    book1_quantity.setTextContent("2");
    book1.appendChild(book1_quantity);

    Element book2 = doc.createElement("book");
    book2.setAttribute("id", "2");
    books.appendChild(book2);

    Element book2_name = doc.createElement("name");
    book2_name.setTextContent("Call of the wind");
    book2.appendChild(book2_name);

    Element book2_quantity = doc.createElement("quantity");
    book2_quantity.setTextContent("3");
    book2.appendChild(book2_quantity);

    Element book3 = doc.createElement("book");
    book3.setAttribute("id", "3");
    books.appendChild(book3);

    Element book3_quality = doc.createElement("quality");
    book3_quality.setTextContent("Good");
    book3.appendChild(book3_quality);

Upvotes: 2

mhaller
mhaller

Reputation: 14222

If the problem is this, then you've declared multiple local variables with the same name. In all programmling languages, you can only have one variable with the same name declared in the same scope. A scope is usually enclosed by curly braces. You can use the same variable name in the same method if you indent them with additional scopes, e.g. like the example below.

However, you should think about the naming of your variables, or if you should instead make use of loop statements. You could also number your variables, e.g. name1, name2, name3 etc.

alt text

If you really want to have multiple variables with the same name, you can separate them by unnamed code blocks by just using the curly braces like this:

Element book = doc.createElement("book");
rootElement.appendChild(book);

{
  Element name = doc.createElement("name");
  name.appendChild(doc.createTextNode("Gone with the wind"));
  book.appendChild(name);
}
{
  Element name = doc.createElement("name");
  name.appendChild(doc.createTextNode("Call of the wind"));
  book.appendChild(name);
}
...

Both name variables life in seprate scopes, so they don't interfere with each other and will not lead to the "Duplicate local variable name" compiler error message.

Upvotes: 0

Related Questions