У меня есть комментарии в моем xml файле, назовите его X. Я беру из другого xml файла Y, и я хочу поместить элементы под правильными комментариями в X. Я использую метод java appendChild. "RootElement" в этом случае будут зависимыми. Он помещает дочерний элемент в нижнюю часть тега rootElement в X. Можно ли это сделать?
<dependencies defaultconf="compile" defaultconfmapping="*->default">
<!--COMPILE-->
<!-- Compile dependencies are available in all classpaths of a project. -->
<!-- These dependencies are included in the war 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
, так что я предполагаю, что вы есть), вы можете использовать insertBefore
а не appendChild
, используя свойство nextSibling
элемента, который вы хотите поместить после него.
Например, если theCommentNode
является узлом, с которого вы хотите поместить newStuff
, то:
theCommentNode.parentNode.insertBefore(newStuff, theCommentNode.nextSibling);
(theCommentNode.parentNode
- предположительно rootElement
, из вашего цитированного XML.)
Если nextSibling
имеет значение null
, это будет добавлено в конце; если нет, то он newStuff
между theCommentNode
и тем, что раньше было его ближайшим родственником.