为什么我的 XML 文件输出追加为'/>'



这是我的困境:

我有一个 XML,我想在其中插入animation_sequence,但是,代码添加了带有左尖括号的 animation_sequnce/>,我可以添加除该元素之外的所有其他元素。为什么?我尝试在此处添加 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);

您向我们展示的代码在树中创建节点。它不会在任何内容后附加任何尖括号。尖括号仅在序列化树(将其转换为词法 XML)时出现。通常,系统会负责如何序列化 XML,并且您无需担心它在序列化它的不同方式之间进行选择,因为在分析 XML 时,差异无关紧要。

现在可能是"/>"是一种症状,表明您构建的树不是您打算构建的树,但那是另一回事。

最新更新