Attached are two screen shots, and my code. I am trying to get the animation_sequence as a closing and ending bracket, I’ve tried my best but now I seek your help. Please help. The code I have produces it as a
Code:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document testDoc = builder.parse(new File("./data/sprite_types/" + spriteName + "/" + spriteName + ".xml"));
Element state = testDoc.createElement("state");
state.setTextContent(element);
Element animationState = testDoc.createElement("animation_state");
Element sequence = testDoc.createElement("animation_sequence");
testDoc.getElementsByTagName("animations_list").item(0).appendChild(animationState).appendChild(state);
testDoc.getElementsByTagName("animation_state").item(testDoc.getElementsByTagName("state").getLength() - 1).appendChild(sequence);
DOMSource source = new DOMSource(testDoc);
PrintStream ps = new PrintStream("./data/sprite_types/" + spriteName + "/" + spriteName + ".xml");
StreamResult result = new StreamResult(ps);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);


The reason is that you’re creating the
animation_sequenceelement but not creating any child node under it. Effectively, you are creating an empty element. Empty elements, when serialized to string, will always appear as<tag/>, rather than<tag></tag>.To avoid this, you need to add an empty
Textnode as a child node of theanimation_sequenceelement:(Or, if you use Commons Lang, replace
""withStringUtils.EMPTY)