Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3624226
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:28:53+00:00 2026-05-18T23:28:53+00:00

For example,I want to generate the following xml file by using java with DOM

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:28:54+00:00Added an answer on May 18, 2026 at 11:28 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate xml file from Struts action object. So that I have
I am using xjc to generate Java classes from the XML schema and the
I want to generate a number based on a distributed probability. For example, just
Example: Suppose in the following example I want to match strings that do not
I have a XML file similar to the following: <a> <b value=a123 /> <b
I want to generate a fn totally at runtime (i.e. the name and the
I want to generate a dynamic site using Zend_Layout. My layout (/application/layouts/scripts/layout.phtml) contains the
I have two XML examples that I want to write a schema for: Example
Background: I want to take some xml from one file, put it in a
I want to generate a random password that has the following pattern: Capital, small,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.