0
DOM을 사용하여 루트 요소에 자식 요소를 추가하는 방법. 여기 내 XML 파일입니다.android에서 DOM을 사용하여 하위 요소를 루트 요소에 추가하는 방법은 무엇입니까?
<?xml version="1.0" encoding="utf-8"?>
<array>
<recipe>
<name>Name First</name>
<description>Description First</description>
<instruction>Instruction First</instruction>
</recipe>
<recipe>
<name>Name Second</name>
<description>Description Second</description>
<instruction>Instruction Second</instruction>
</recipe>
</array>
나는 <array>
태그의 자식으로 새로운 <recipe>
태그를 추가 할 수 있습니다. 여기에 내가 개발 한 자바 코드가 있습니다.이 코드는 로그 메시지가 제대로 표시되고 오류가 없지만 새로운 자식을 추가하지 않습니다. 제발 도와주세요. 그리고 어디서 잘못되었는지 확인합니다. 미리 감사드립니다.
private void addRecipeToMyRecipeFile(MyRecipeModel recipeModel)
{
MyRecipeModel mRecipe = recipeModel;
MyRecipeHandler recipeHandler = new MyRecipeHandler();
// Here I get the content of XML file as a string and convert the string in XML format
Document doc = convertRecipesFileIntoXML(recipeHandler.getContentOfMyRecipesFileFromSDCard());
Log.e("Doc", "convertRecipesFileIntoXML");
final NodeList nodes_array = doc.getElementsByTagName(TAG_ARRAY);
//We have encountered an <array> tag.
Element rootArrayTag = (Element)nodes_array.item(0);
Log.e("Element", "Array");
// <recipe> elements
Element recipe = doc.createElement(TAG_RECIPE);
rootArrayTag.appendChild(recipe);
Log.e("Element", "Recipe");
// <name> is name of recipe
Element name = doc.createElement(TAG_RECIPE_NAME);
name.appendChild(doc.createTextNode(mRecipe.getMyRecipeName()));
recipe.appendChild(name);
Log.e("Element", "Name");
// <description> is description of the recipe
Element description = doc.createElement(TAG_RECIPE_DESCRIPTION);
description.appendChild(doc.createTextNode(mRecipe.getMyRecipeDescription()));
recipe.appendChild(description);
Log.e("Element", "Description");
// <instructions> elements
Element instructions = doc.createElement(TAG_RECIPE_INSTRUCTION);
instructions.appendChild(doc.createCDATASection(mRecipe.getMyRecipeInstruction()));
recipe.appendChild(instructions);
Log.e("Element", "Instruction");
}