0

附上两个屏幕截图和我的代码。我正在尝试将 animation_sequence 作为结束和结束括号,我已尽力而为,但现在我寻求您的帮助。请帮忙。我的代码将其生成为

代码:

 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);

不好 好的

4

1 回答 1

1

原因是您正在创建animation_sequence元素,但没有在其下创建任何子节点。实际上,您正在创建一个空元素。空元素,当序列化为字符串时,将始终显示为<tag/>,而不是<tag></tag>

为避免这种情况,您需要添加一个空Text节点作为animation_sequence元素的子节点:

sequence.appendChild(testDoc.createTextNode(""));

(或者,如果您使用 Commons Lang,请替换""StringUtils.EMPTY

于 2012-11-30T03:48:41.553 回答