【问题标题】:Read And Write XML file content to another XML file below a certain tag读取和写入 XML 文件内容到某个标签下的另一个 XML 文件
【发布时间】:2021-08-04 06:05:49
【问题描述】:

伙计们,我只是想知道如何在某个标签以 JAVA 语言结尾后,如何从一个 xml 文件中复制内容并将其写入下面的另一个 xml 文件。

这是我的 xmlfile1:

 <dependencies>
    <dependency>
      <groupId>com.eventlistener</groupId>
      <artifactId>Pel</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

xmlfile2:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>autointelli</groupId>
  <artifactId>proj_autointelli_internal_gopalakrishnan_execute_a_given_command_and_send_output_in_email</artifactId>
  <version>19.0</version>
  <name>proj_autointelli_internal_gopalakrishnan_execute_a_given_command_and_send_output_in_email</name>
  <description>new project</description>
  <repositories>
    <repository>
      <id>guvnor-m2-repo</id>
      <name>Guvnor M2 Repo</name>
      <url>/maven2/</url>
    </repository>
  </repositories>
</project>

需要在描述标签结束后将xmlfile1的内容粘贴到xmlfile2的正下方,而不覆盖已经存在的内容。

【问题讨论】:

    标签: java xml xml-parsing


    【解决方案1】:

    这种事情使用 XSLT 是最简单的。假设 XSLT 3.0(在 Saxon 库中可用),使用

    转换 xmlfile2
    <transform version="3.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <mode on-no-match="shallow-copy"/>
    <template match="description">
      <copy-of select="., doc('xmlfile1.xml')"/>
    </template>
    </transform> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 2020-12-22
      相关资源
      最近更新 更多