1

I am trying to take user input from a console and feed that into a XML file. Evrytime the user moves on to the next line I want to take the string they typed in and create a new element. Here is what I am trying to achieve:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<note>
    <header>
        <Tempo Laya="2"/>
    </header>
    <Notes>
    <n1 Bol="Text the user entered"/>
    <n2 Bol="Text the user entered over the next iteration"/>
    <n3 Bol="Text the user entered over the next iteration"/>
    </Notes>
</note>

I thought the best way to do this would be to create these elements, however; I am not able to create unique element names through this. Here is my code so far:

//Create note element
    Element notes = doc.createElement("Notes");
    rootElement.appendChild(notes);
    System.out.println("Input your notes matraa by maatra. To proceed to the next maatra press ENTER. \n To exit enter END");
    do{
        int noteCount = 1;
        System.out.println("Maatra: ");
        bol = scanner.nextLine();


    }while(scanner.nextLine()!= "END");

Is there any way to create and append the elements using the loop above. If not how else could I do it?

M A
  • 71,713
  • 13
  • 134
  • 174
user3131493
  • 53
  • 1
  • 7
  • You shouldn't number your nodes like that. Name them all `Note`, and whatever code cares about their order can determine it by their order within the Notes node, or you can add a "number" attribute – Bohemian Feb 28 '16 at 20:54

2 Answers2

4
  1. First you have a bug when checking the "END" string. You should use String.equals instead of reference equality. Also calling nextLine() twice reads two lines instead of one. Just check the first read line:

    while(bol.equals("END"));
    
  2. Second it would be much easier to use JAXB to perform this task. Whenever a new input is added by the user, you add a new object in memory, as opposed to manually handling the document tree. Once the "END" is received, the contents are marshalled as a document.

    You can read an introduction to JAXB in the Oracle tutorials.

  3. If you still want to use standard DOM to do this, you would need to do something like:

    Element noteElement = doc.createElement("note");
    notes.appendChild(noteElement);
    

    This means, as @Andreas commented below, the elements should ideally have the same name, not sequentially numbered (n1, n2, n3, ...). If numbering is really needed, you can add another attribute containing the id (using the method createAttribute in Document similar to createElement).

M A
  • 71,713
  • 13
  • 134
  • 174
  • 1
    Nice list to start with. Let me add to it: 4) Calling `nextLine()` twice in the loop is not correct. Check `bol` for value `"END"` and `break` the loop, and make the loop itself unending, e.g. `for (;;) { ... }`. – Andreas Feb 28 '16 at 16:09
  • 1
    5) The child elements with user notes should all be called the same, not sequentially numbered (`n1`, `n2`, `n3`, ...). If numbering is really needed, add another attribute. The "schema" of an XML file is static, so dynamically inventing new elements is bad. – Andreas Feb 28 '16 at 16:12
  • Please add to your answer, if you like my additions. ;-) – Andreas Feb 28 '16 at 16:14
0

I recommend you to create a object "Note" and use JAXB. See this tutorial: Tutorial. It's easy to convert object to XML and XML to object.