Java xml注释后的appendChild(childElement)

Java xml注释后的appendChild(childElement),java,xml,Java,Xml,我的xml文件中有注释,称之为X。我从另一个xml文件Y中获取,我希望将元素放在X中正确的注释下。我使用java appendChild方法。本例中的“rootElement”将是依赖项。它将childElement放置在X中rootElement标记的底部。可以这样做吗 <dependencies defaultconf="compile" defaultconfmapping="*-&gt;default"> <!--COMPILE--> &l

我的xml文件中有注释,称之为X。我从另一个xml文件Y中获取,我希望将元素放在X中正确的注释下。我使用java appendChild方法。本例中的“rootElement”将是依赖项。它将childElement放置在X中rootElement标记的底部。可以这样做吗

<dependencies defaultconf="compile" defaultconfmapping="*-&gt;default">
    <!--COMPILE-->
    <!-- Compile dependencies are available in all classpaths of a project. -->
    <!-- These dependencies are included in the war's WEB-INF/lib and are propagated to     dependent projects. -->
    <!-- This is the default config and is used if no config is specified. -->

    <!--PROVIDED-->`
    <!-- This is much like compile, but indicates you expect the JDK or the tomcat container to provide the dependency at runtime. -->
    <!-- This scope is only available on the compilation and test classpaths, and is not transitive. -->
    <!-- NOTE: You must add conf="provided" to the dependency -->

`

是。假设您使用的库支持DOM方法(您使用的是
appendChild
,所以我猜您是这样),那么您可以使用要放在后面的元素的属性,而不是
appendChild

例如,如果
commentnode
是要将
newStuff
放在后面的节点,则:

theCommentNode.parentNode.insertBefore(newStuff, theCommentNode.nextSibling);
commentNode.parentNode
可能是引用的XML中的
rootElement

如果
nextSibling
null
,则会在末尾追加;如果没有,它将把
newStuff
放在
commentnode
和它的下一个兄弟节点之间