[英]How to use non-standard variable names in Java for producing XML tags?
I am trying to write the AndroidManifest.xml file programmatically through Java using Jaxb. 我正在尝试使用Jaxb通过Java以编程方式编写AndroidManifest.xml文件。 The problem comes while defining the attributes, which follow a string:string naming convention.
在定义属性时会出现问题,这些属性遵循string:string命名约定。 For example, the manifest element is defined thus:
例如,清单元素的定义如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="string"
android:sharedUserId="string"
android:sharedUserLabel="string resource"
android:versionCode="integer"
android:versionName="string"
android:installLocation=["auto" | "internalOnly" | "preferExternal"] >
. . .
</manifest>
While doing this from Java, I cannot annotate the attributes because xmlns:android etc are not valid Java variables. 从Java执行此操作时,我无法注释属性,因为xmlns:android等不是有效的Java变量。
What is the best way to overcome this, while still using Jaxb, and not resorting to StringBuilder technique for generating the XML? 在仍然使用Jaxb并且不求助于StringBuilder技术生成XML的同时,克服此问题的最佳方法是什么?
Here is an example from the javadoc of javax.xml.bind.annotation.XmlElement: 这是javax.xml.bind.annotation.XmlElement的javadoc中的示例:
@XmlElement(name="item-price")
public java.math.BigDecimal price;
The name parameter is what you'll see in the XML. name参数是您将在XML中看到的。
But this android: the namespace prefix and should appear according to the namespace definition, which is the parameter of namespace: 但是这个android:名称空间前缀,应该根据名称空间定义出现,这是名称空间的参数:
@XmlElement(name="item-price", namespace="http://schemas.android.com/apk/res/android")
public java.math.BigDecimal price;
After marshalling, you'll most likely see "ns1:" in place of android:, but that's OK. 编组后,您很可能会看到“ ns1:”代替android :,但这没关系。
Easiest way to get the annotated Java code: Write (or find) the XML schema and run it through xjc
. 获取带注释的Java代码的最简单方法:编写(或查找)XML模式并通过
xjc
运行它。 Works all the time - almost ;-) 一直在工作-几乎;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.