Я уже прочитал несколько ответов на этом сайте, но ни один из них не работал для меня. У меня есть XML-файл, как это:Java, добавляющий данные XML
<root>
<character>
<name>Volstvok</name>
<charID>(omitted)</charID>
<userID>(omitted)</userID>
<apiKey>(omitted)</apiKey>
</character>
</root>
мне нужно добавить еще <character>
каким-то образом.
Я пытаюсь это, но он не работает:
public void addCharacter(String name, int id, int userID, String apiKey){
Element newCharacter = doc.createElement("character");
Element newName = doc.createElement("name");
newName.setTextContent(name);
Element newID = doc.createElement("charID");
newID.setTextContent(Integer.toString(id));
Element newUserID = doc.createElement("userID");
newUserID.setTextContent(Integer.toString(userID));
Element newApiKey = doc.createElement("apiKey");
newApiKey.setTextContent(apiKey);
//Setup and write
newCharacter.appendChild(newName);
newCharacter.appendChild(newID);
newCharacter.appendChild(newUserID);
newCharacter.appendChild(newApiKey);
doc.getDocumentElement().appendChild(newCharacter);
}
Это сработало, спасибо! Теперь, если это не так уж сложно, можете ли вы сказать мне, как удалить на основе его местоположения в XML-файле? –
Travis