1

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?

Eugene
  • 1,037
  • 4
  • 20
  • 34

1 Answers1

0

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.

vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30
  • Tried to exclude `xmlModifier.bind(vtdNav);` from `writeElement()` and move it into class constructor. Now I have `com.ximpleware.ModifyException: There can be only one insert per offset` exception. What is that? – Eugene Oct 02 '13 at 16:07
  • 1
    Yes, vtd is different from Dom in terms of modification. You are supposed to insert a single byte array per offset value. This means you have to do the text concatnatio in your code, then insert everything after an element. – vtd-xml-author Oct 03 '13 at 04:58