You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Maven将XML文件转换为PNG等格式的图片?

Maven方案:XML自动转PNG并集成到文档体系

我之前刚好折腾过类似的需求——频繁改XML还要自动生成可视化图放进文档,给你两个经过验证的Maven方案,直接套就行:

方案1:通用XML转结构示意图(适配任意XML)

这个方案适合把任意XML的层级结构转成PNG示意图,核心是用XSLT把XML转成Graphviz的dot格式,再调用Graphviz生成图片:

步骤分解

  1. 写XSLT样式表:把你的XML节点、属性映射成Graphviz的节点和连线(比如把父节点和子节点用箭头连起来),保存成src/main/resources/xml-to-dot.xsl
  2. 用Maven插件先转XML为dot文件,再调用Graphviz生成PNG

Pom.xml配置示例

先加XSLT转换插件,把XML转成dot:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>xml-maven-plugin</artifactId>
    <version>1.0.2</version>
    <executions>
        <execution>
            <id>xml-to-dot</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>transform</goal>
            </goals>
            <configuration>
                <transformationSets>
                    <transformationSet>
                        <dir>src/main/resources</dir>
                        <includes>
                            <include>your-config.xml</include>
                        </includes>
                        <stylesheet>src/main/resources/xml-to-dot.xsl</stylesheet>
                        <outputDir>${project.build.directory}/generated-resources</outputDir>
                        <fileMappers>
                            <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
                                <targetExtension>.dot</targetExtension>
                            </fileMapper>
                        </fileMappers>
                    </transformationSet>
                </transformationSets>
            </configuration>
        </execution>
    </executions>
</plugin>

再加exec插件调用Graphviz生成PNG:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>dot-to-png</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>dot</executable>
                <arguments>
                    <argument>-Tpng</argument>
                    <argument>${project.build.directory}/generated-resources/your-config.dot</argument>
                    <argument>-o</argument>
                    <argument>${project.build.directory}/generated-docs/xml-structure.png</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

方案2:PlantUML XML直接转图(适合UML类图/时序图)

如果你的XML是PlantUML的XML格式(比如用PlantUML的XML语法描述UML图),直接用官方Maven插件一步到位,不用装额外工具:

Pom.xml配置示例

<plugin>
    <groupId>net.sourceforge.plantuml</groupId>
    <artifactId>plantuml-maven-plugin</artifactId>
    <version>1.2024.2</version>
    <executions>
        <execution>
            <id>plantuml-xml-to-png</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <sourceFiles>
                    <directory>src/main/resources/uml</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </sourceFiles>
                <outputDirectory>${project.build.directory}/generated-docs</outputDirectory>
                <format>png</format>
                <!-- 可选:设置图片分辨率 -->
                <dpi>150</dpi>
            </configuration>
        </execution>
    </executions>
</plugin>

集成到自动文档体系

不管用哪个方案,生成的PNG都在target/generated-docs目录里,你可以:

  • 用maven-site-plugin:把这个目录配置成site的资源目录,在site页面里引用图片
  • 用Asciidoctor Maven插件:在.adoc文档里直接写image::../target/generated-docs/xml-structure.png[XML结构示意图],构建时会自动把图片打包进最终文档

关键提示

  • 方案1需要本地安装Graphviz(确保dot命令在系统PATH里)
  • 把插件绑定到generate-resources阶段,这样每次执行mvn clean install或者mvn site,都会自动重新生成图片(只要XML有修改)
  • 如果是自定义XML结构,方案1的XSLT需要你自己调整,网上有很多XML转dot的XSLT模板可以参考

内容的提问来源于stack exchange,提问作者Walter Kuhn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:34:31