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

  • SEARCH
  • Home
  • 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 8884345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:02:33+00:00 2026-06-14T21:02:33+00:00

Okay so I’m creating a users class which asks for input then stores it

  • 0

Okay so I’m creating a users class which asks for input then stores it in an XML file using java. I figured out to create the original XML file I think but I’m have trouble figuring out how to add a new user with the attribute “id” of one more then the previous User entry.

Here is the code I have so far:

/*imports */

    public class CreateUser {   
        static Scanner input = new Scanner(System.in);

/* object names*/
    String name;
    String age;
    String bday;
    String gender;
    String location;
    String orientation;
    String relationship;
    String hobbies;
    String choice;
    String username;
    String password;

    public void makeUser(){

/*left out code to get user entries here 

seemed irrelevant/*

    /*checks for file if it doesn't exist then it creates it else it should append
 the user to the xml document with a id increase of one.
 The appending part I'm not sure how to do.*/
File f = new File("C:\\Users\\Steven\\Workspace\\twitter\\src\\users.xml");
            if(f.exists()) { 
                try {
                    /* need help here*/
                }
            }
            else{
                try{
                    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                    Document users = docBuilder.newDocument();

                    Element user = users.createElement("user");
                    users.appendChild(user);

                    Attr attr = users.createAttribute("id");
                    attr.setValue("0");
                    user.setAttributeNode(attr);

                    Element dname = users.createElement("name");
                    dname.appendChild(users.createTextNode(name));
                    user.appendChild(dname);

                    Element dgender = users.createElement("gender");
                    dgender.appendChild(users.createTextNode(gender));
                    user.appendChild(dgender);

                    Element dlocation = users.createElement("location");
                    dlocation.appendChild(users.createTextNode(location);
                    user.appendChild(dlocation);

                    Element dorientation = users.createElement("orientation");
                    dorientation.appendChild(users.createTextNode(orientation));
                    user.appendChild(dorientation);

                    Element drelationship = users.createElement("relationship");
                    drelationship.appendChild(users.createTextNode(relationship));
                    drelationship.appendChild(drelationship);


                    Element dhobbies = users.createElement("hobbies");
                    dhobbies.appendChild(users.createTextNode(hobbies));
                    dhobbies.appendChild(dhobbies);

                    Element dchoice = users.createElement("choice");
                    dchoice.appendChild(users.createTextNode(choice));
                    dchoice.appendChild(dchoice);

                    Element dusername = users.createElement("username");
                    dusername.appendChild(users.createTextNode(username));
                    dusername.appendChild(dusername);

                    Element dpassword = users.createElement("password");
                    dpassword.appendChild(users.createTextNode(password));
                    dpassword.appendChild(dpassword);

                    Element dbday = users.createElement("birthday");
                    dbday.appendChild(users.createTextNode(bday));
                    dbday.appendChild(dbday);

                    Element dage = users.createElement("age");
                    dage.appendChild(users.createTextNode(age));
                    dage.appendChild(dage);

                    TransformerFactory transformerFactory = TransformerFactory.newInstance();
                    Transformer transformer = transformerFactory.newTransformer();
                    DOMSource source = new DOMSource(users);
                    StreamResult result = new StreamResult(new File("C:\\Users\\Steven\\Workspace\\twitter\\src\\users.xml"));

                    StreamResult test = new StreamResult(System.out);
                    transformer.transform(source, result);
                } catch (ParserConfigurationException pce) {
                    pce.printStackTrace();
                } catch (TransformerException tfe) {
                    tfe.printStackTrace();
                }
            }
        }

I know its a lot of code to look through and I don’t want an exact coded answer but maybe just how to append the user with the attribute value one more then the previous entry. Or a point in a the direction of a helpful website. Anything really I’ve been perplexed for a little and I feel like I should get something this simple. Thanks in advance for any help

  • 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-14T21:02:34+00:00Added an answer on June 14, 2026 at 9:02 pm

    In your first section(if block), I think you can open your file in append mode as below to add an user, assuming user node is not wrapped in another node.

     StreamResult result = new StreamResult(
        new FileWriter("C:\\Users\\Steven\\Workspace\\twitter\\src\\users.xml", true));
    

    There are two changes in above statement:

    1. Using FileWriter in place of File
    2. Using a second parameter true, which open the file in append mode.

    EDIT: To get the max existing ID, you need to read file and look for ID tag as below:

      File xmlFile = new File("C:\\Users\\Steven\\Workspace\\twitter\\src\\users.xml");
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(xmlFile);
      doc.getDocumentElement().normalize();
      NodeList nList = doc.getElementsByTagName("userId");//use the id tag name
      int maxId = 0;
      for(Node node: nList){
          if(Integer.parseInt(node.getTextContent()) > maxId ){
            maxId = Integer.parseInt(node.getTextContent());
          }
      }
      int newId = maxId +1; //use this ID
      xmlFile.close();//close the file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay say your using a app, and you opened a new activity and then
Okay so I can't figure this out. Like a file that I using grep
Okay, next PHPExcel question. I have an HTML form that users fill out and
Okay, so I'm working on a java server for an Apps backend, it must
Okay, here is one weird bug... In my app, I load multiple xml files.
Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My
Okay, I've set up a bit of code which searching for all the pages
Okay, I am using a custom tab bar (not a tab based app) but
Okay. So I'm creating this app and I'm really happy with it apart from
Okay so I'm using the jQuery autocomplete working with my database of locations and

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.