简体   繁体   English

如何在Java中使用非标准变量名来生成XML标签?

[英]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.

相关问题 如何在Java中的XML中取消转义非标准字符? - How to unescape non-standard characters in XML in Java? Java更改和移动非标准XML文件 - Java change and move non-standard XML file 如何使用 DateTimeFormatter 解析非标准月份名称 - How to parse non-standard month names with DateTimeFormatter 使用Java,如何在Android体系结构中引用非标准类型xml对象? - Using Java, how can I reference a non-standard type xml object in the Android architecture? 如何故意创建非标准或格式错误的Java URL对象 - How to Create non-Standard or Malformed Java URL object deliberately 如何使用Maven测试具有非标准文件结构的代码? - How to use Maven to test code with a non-standard file structure? 使用XML发送非标准字符 - Sending non-standard characters in XML 是否有标准的Java SE HTML解析器? 如果是这样,为什么要使用非标准的呢? - Is there a Standard Java SE HTML Parser? If so, why use non-standard ones? 在 Spring 集成(入站网关)中传递非标准 Header 名称 - Passing non-standard Header names in Spring Integration (inbound gateway) Maven pom.xml如何在非标准项目结构中识别出Testng测试用例? - How maven pom.xml identifies the testng test cases in a non-standard project structure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM