Question

I want to write several elements inside one parent and I have the following code:

public void writeElement(String parent, String element, String content) {
    try {
        xmlModifier.bind(vtdNav);
        vtdNav.toElement(VTDNav.FC, parent);
        xmlModifier.insertAfterHead("<" + element + ">" + content + "</" + element + ">");
        xmlModifier.output(filepath);
    } catch (ModifyException | NavException | IOException | TranscodeException e) {
        e.printStackTrace(); 
    }
}

and calling method:

 @FXML
public void save() {
    String surname = surnameField.getText();
    String name = nameField.getText();
    String patronymic = patronymicField.getText();
    String id = idField.getText();
    String diagnosis = diagnosisArea.getText();
    String comments = commentsArea.getText();

    dbFile = DBFile.setDoc(dbDir + dbChoiceBox.getValue(), false);
    dbFile.writeElement("db", "patient", "");
    dbFile.writeElement("patient", "surname", surname);
    dbFile.writeElement("patient", "name", name);
    dbFile.writeElement("patient", "patronymic", patronymic);
    dbFile.writeElement("patient", "id", id);
    dbFile.writeElement("patient", "diagnosis", diagnosis);
    dbFile.writeElement("patient", "comments", comments);
}

but instead this result:

<db>
<patient>
<surname></surname>
<name></name>
...
</patient>
</db>

I have only this:

<db>
<patient>
<comments></comments>
</patient>
</db>

It looks like writeElement rewrite the same element each time. Why this is happening and how can I fix it?

Was it helpful?

Solution

Because you are outputting an XML file for every single modification. If you want to make all modifications all at once, you should not call XMLModifier's output every time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top