我的XML输出不是我想要的

时间:2012-12-02 03:44:21

标签: xml

附上两张截图和我的代码。我试图将animation_sequence作为结束和结束括号,我已尽力而为,但现在我寻求你的帮助。这是我的尝试,我决定这样做,但现在我的状态,如“雅虎”显示为“雅虎”,我知道这可能与动画序列的setTextContent有关(虽然我不明白它是怎么做的在我的状态内容中添加空格。我的问题是我想要的只是我的动画序列有结束和结束括号。我通过在if语句中寻找两个额外的空格来解决,但我觉得这是一个可悲的方法,它的有点像接受失败。 我附加的第二张图片是我希望实现的,而不会给我的状态增加额外的“”。如果没有下面的代码(特定的设置文本内容部分),它会生成图像1。就在我以为我解决了它的时候,这两个空间正在扼杀我。如果您需要任何额外的代码,请告诉我。我需要你的帮助。

Element animationState = testDoc.createElement("animation_state");
        Element sequence = testDoc.createElement("animation_sequence");
        sequence.setTextContent(" ");
        sequence.appendChild(testDoc.createTextNode(" "));
testDoc.getElementsByTagName("animations_list").item(0).appendChild(animationState).appendChild(state);
        testDoc.getElementsByTagName("animation_state").item(testDoc.getElementsByTagName("state").getLength() - 1).appendChild(sequence);


   for(int i = 0; i<testDoc.getElementsByTagName("animation_state").getLength(); i++)
   {
       if(  testDoc.getElementsByTagName("animation_state").item(i).getTextContent().
            equals(Poseur.getPoseur().getAnimatedSpriteViewer().getSpriteStateComboBox().
            getSelectedItem().toString())||(testDoc.getElementsByTagName("animation_state").item(i).getTextContent()).equals(Poseur.getPoseur().getAnimatedSpriteViewer().getSpriteStateComboBox().getSelectedItem().toString()+"  "))
       {
           testDoc.getElementsByTagName("animation_sequence").item(i).appendChild(poseImage);
       }
   }

not good good

1 个答案:

答案 0 :(得分:1)

在您创建animation_sequence节点时,在Java中显示,您正在使用抽象树结构。此时,没有打开或关闭标签,只有节点。

“标签”(打开和关闭)的存在完全是XML文档的序列化的一个方面,即如何将其转换为输出的文本形式。就标准而言,序列化<animation_sequence/><animation_sequence></animation_sequence>无法区分,因此允许序列化程序编写任何一个。

某些序列化程序将允许您以速记形式(尾随/)或打开/关闭标记对指定要写出空节点的方式。您需要研究特定的XML库,并确定序列化程序是否提供了这样的选项。

相关问题