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 7805277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:10:29+00:00 2026-06-02T02:10:29+00:00

It’s a simple problem. but i am not able to debug it. I have

  • 0

It’s a simple problem. but i am not able to debug it. I have class “Adding.java” that adds some data to ArrayList

public class Adding {

WriteFile ob = new WriteFile();

ArrayList list = new ArrayList();
public void add(){

    list.add("Tim");
    list.add(2333);
    list.add(23);

    list.add("John");
    list.add(423);
    list.add(23);

    ob.writeXmlFile(list);

    } }

and another class “WriteFile.java” that creates a xml file

public class WriteFile {

public void writeXmlFile(ArrayList<Object> list) {

    try {

        DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
        DocumentBuilder build = dFact.newDocumentBuilder();
        Document doc = build.newDocument();

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

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


        for(int i=0; i<list.size(); i ++ ) {

            Element name = doc.createElement("Name");
            name.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
            Details.appendChild(name);

            Element id = doc.createElement("ID");
            id.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
            Details.appendChild(id);


            Element mmi = doc.createElement("Age");
            mmi.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
            Details.appendChild(mmi);

        }


         // Save the document to the disk file
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();

        // format the XML nicely
        aTransformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");

        aTransformer.setOutputProperty(
                "{http://xml.apache.org/xslt}indent-amount", "4");
        aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");



        DOMSource source = new DOMSource(doc);
        try {
            FileWriter fos = new FileWriter("/home/ros.xml");
            StreamResult result = new StreamResult(fos);
            aTransformer.transform(source, result);

        } catch (IOException e) {

            e.printStackTrace();
        }



    } catch (TransformerException ex) {
        System.out.println("Error outputting document");

    } catch (ParserConfigurationException ex) {
        System.out.println("Error building document");
    }

when i execute this i get following output

  <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
       <Studentinfo>
             <Details>
                  <name>Tim</name>
                  <Id>Tim</Id>
                  <age>Tim</age>
                  <name>2333</name>
                  <Id>2333</Id>
                  <age>2333</age>
                  ..... 
              </Details>
        </studentinfo>

and so on. But I want the final output to be of this form

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
       <Studentinfo>
             <Details>
                  <name>Tim</name>
                  <Id>2333</Id>
                  <age>23</age>

                  <name>John</name>
                  <Id>423</Id>
                  <age>2333</age>
                  <size>23</size>
             </Details>
        </studentinfo>

Is there any problem with “for” loop to iterate over “list” element ?? . Any help is appreciated. Thanks in advance

  • 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-06-02T02:10:30+00:00Added an answer on June 2, 2026 at 2:10 am

    Why don’t you store Student objects in your list, each having a name, an ID and an age? This would be much more maintainable, and easy to program?

    At the moment, you use the same index in the list to find the three properties. You would need to iterate by steps of 3 in the loop, and get the elements i, i+1 and i+2 to make it work.

    list.add(new Student("Tim", 2333, 23));
    list.add(new Student("John", 423, 23));
    
    ...
    
    for (Student student : studentList) {
        ...
        name.appendChild(doc.createTextNode(student.getName()));
        ...
        id.appendChild(doc.createTextNode(Integer.toString(student.getId())));
        ...
        age.appendChild(doc.createTextNode(Integer.toString(student.getAge())));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I have some data like this: 1 2 3 4 5 9 2 6
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to

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.