以下是从Apache Maven configuration
中提取的4.0.0 POM model xsd元素的描述。
<xs:element minOccurs="0" name="configuration">
<xs:annotation>
<xs:documentation source="version">0.0.0+</xs:documentation>
<xs:documentation source="description">
<p>The configuration as DOM object.</p> <p>By default, every element content is trimmed, but starting with Maven 3.1.0, you can add <code>xml:space="preserve"</code> to elements you want to preserve whitespace.</p> <p>You can control how child POMs inherit configuration from parent POMs by adding <code>combine.children</code> or <code>combine.self</code> attributes to the children of the configuration element:</p> <ul> <li><code>combine.children</code>: available values are <code>merge</code> (default) and <code>append</code>,</li> <li><code>combine.self</code>: available values are <code>merge</code> (default) and <code>override</code>.</li> </ul> <p>See <a href="http://maven.apache.org/pom.html#Plugins">POM Reference documentation</a> and <a href="http://plexus.codehaus.org/plexus-utils/apidocs/org/codehaus/plexus/util/xml/Xpp3DomUtils.html">Xpp3DomUtils</a> for more information.</p>
</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
</xs:sequence>
</xs:complexType>
</xs:element>
我使用以下绑定文件从上面的xsd生成类:
<jxb:bindings version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:extensionBindingPrefixes="xjc"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<jxb:globalBindings>
<!-- use plural method names for repeatable elements -->
<xjc:simple />
</jxb:globalBindings>
<jxb:bindings schemaLocation="maven-4.0.0.xsd">
<!-- rename all the node of type "any" to "elements" to improve readability -->
<jxb:bindings multiple="true" node="//xs:any" >
<jxb:property name="elements"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
这很好用。例如,它生成一个Configuration
类,它具有返回getElements()
实例的Element
方法。
但是,当我遇到configuration
标记时,我希望生成的Configuration
类能够将所有底层元素的XML保存为String(因为它是在解析器XML文件中编写的,保留注释,换行,空白行和空格。)
例如,假设我有以下XML文件:
<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>test</groupId>
<artifactId>test</artifactId>
<version>1.0.1-SNAPSHOT</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${plugin.maven-compiler-plugin.version}</version>
<configuration>
<source>${build.jdk.source.version}</source>
<target>${build.jdk.target.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
在解析了这个XML文件并将其加载到我生成的模型中之后,我希望能够在getXML()
实例上调用Configuration
方法并获得:
<configuration>
<source>${build.jdk.source.version}</source>
<target>${build.jdk.target.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<fork>true</fork>
</configuration>
(错误的缩进保持原样)
我在考虑以下方法
Configuration
类扩展一个XmlHolder
类,能够将XML保存为字符串(使用getDom()
和setXml(String)
方法)XmlHolderAdapter
适配器类将XML块提取为String并将其设置在Configuration
类上。我的问题:
XMLStreamReader
作为源)?非常感谢提前,
我也在过去搜索类似问题的答案,并在运行时需要相关的XSD部分类。我甚至为此写了xjc-documentation-annotation-plugin。
如何普遍使用它你可能会在答案中看到:How to make generated classes contain Javadoc from XML Schema documentation
我也准备示例并测试你的特定问题。请查看at commit例如gradle
项目。
为了充分理解,请一步一步:
gradle
中,我使用gradle-xjc-plugin来调用XJC
(你可以使用类似的maven
插件或直接调用它):plugins {
id 'java'
id 'org.unbroken-dome.xjc' version '1.4.1' // https://github.com/unbroken-dome/gradle-xjc-plugin
}
dependencies {
xjcClasspath 'info.hubbitus:xjc-documentation-annotation-plugin:1.0'
...
}
// Results by default in `build/xjc/generated-sources`
xjcGenerate {
/**
* There:
* 1. CadastralBlock.xsd is minimal example of functional.
* 2. maven-4.0.0.xsd - example by SOq https://stackoverflow.com/questions/42223784/how-can-i-generate-a-class-from-which-i-can-retrieve-the-xml-of-a-node-as-a-stri
*/
source = fileTree('src/main/resources') { include '*.xsd' }
bindingFiles = fileTree('src/main/jaxb') { include '*.xjb' }
outputDirectory = file('src/main/generated-java')
packageLevelAnnotations = false
targetPackage = 'info.hubbitus.xjc.plugin.example'
extraArgs = [ '-XPluginDescriptionAnnotation' ]
}
sourceSets.main.java.srcDir new File(buildDir, xjcGenerate.outputDirectory.absolutePath)
// If you use IntellyJ Idea:
idea {
module {
// Marks the already(!) added srcDir as "generated"
generatedSourceDirs += xjcGenerate.outputDirectory
}
}
./gradlew xjcGenerate
您的课程将在目录src/main/generated-java
中使用@XsdInfo
注释。对于你的情况最感兴趣的xsdElementPart
元素。
Plugin
类包含Configuration
作为内部类): XsdInfo xsdAnnotation = plugin.getClass().getDeclaredAnnotation(XsdInfo.class);
System.out.println(xsdAnnotation.xsdElementPart());
我准备测试TestGeneratedMavenModel
充分展示它。