如何在没有 java 双引号的情况下发送 Jaxb XmlAttribute 数据 [英] How to send Jaxb XmlAttribute data without java double quotes

查看:31
本文介绍了如何在没有 java 双引号的情况下发送 Jaxb XmlAttribute 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在尝试使用 jaxb 从 java 类构建 xml 时遇到以下问题.我的第一个标签需要是这样的:

So I have the following issue when trying to build an xml from java class using jaxb. My first tag needs to be like this :

<Entity Type=‘test-instance’>
Some xml
</Entity>

我需要第一个标签写一些东西,但结束的标签只需要实体.

I need the first tag to have something written in it but the closing one needs to have only Entity.

我创建了一个 claas 并添加了带有 name=Entity 的 @XMLRootElement 标记,并且使用它我有正确的最后一个标记.如果我在第一个标签和结束标签中使用整个表达式,这对我没有帮助.

I created a claas and added @XMLRootElement tag with name=Entity and using this I have the correct last tag. If I use the whole expression I have in both first and closing tag which does not help me.

我用@XMLAttribute 添加了一个String 变量,所以我可以在Entity 之后添加其他东西.问题是如果我用值初始化变量,我会得到字符串双引号.例如

I added a String variable with @XMLAttribute so I can add something else after Entity. The problem is if I initialize the variable with value, I will get the String double quotes. For example

@XMLAttribute(name="Type")
String x = "test-instance";

当我构建它时,它将显示:

When I build this it will read :

<Entity Type="test-instance">

,用双引号,我只需要单引号.

, with double quotes and I need only single quotes.

如果在 xml 属性中使用我需要的完整描述,例如:

If use the whole description I need in the xml attribute, for example :

@XMLAttribute(name="Type=‘test-instance’")
String x;

这可以正常工作,但如果变量没有初始化,那么它不会添加任何东西,如果写类似 String x=";然后我会得到:

This will work fine but if the variable is not initialized then it won’t add anything and if write something like String x=""; Then I will get :

<Entity Type=‘test-instance’="">

所以我无法摆脱来自我的字符串的引号.

So I cannot get rid of the quotes which are coming from my String.

我正在考虑对第一个实体标签进行硬编码,并让我关闭它的最后一个标签有所不同,但找不到任何东西.

I was thinking something to either hardcode the first Entity tag and to have the last one where I close it different but couldn’t find anything.

或者即使字符串没有初始化,也可以设置 XMLAttribute 的另一个选项.我在 XMLElement 上看到了一些类似必需"的东西,但它不适用于 XMLAttribute,我不确定它是否能满足我的需要它到.

Or the other option to set the XMLAttribute even if the String is not initialized.I saw something on the XMLElement that had something like "required" but it’s not available for XMLAttribute and I’m not really sure it does what I need it to.

任何帮助将不胜感激.

谢谢

推荐答案

在XML应用中的原则应该是:

In XML applications the principle should be:

(a) 接收应用程序不应该关心如何编写 XML 的无关紧要的词汇细节.(实现这一点的最佳方法是使用可观的 XML 解析器来读取 XML.)

(a) Receiving applications shouldn't care about the insignificant lexical detail of how the XML is written. (The best way of achieving this is to use a respectable XML parser to read the XML.)

(b) 编写应用程序应该可以自由使用他们认为方便的任何词汇约定.(这意味着您可以使用任何可敬的序列化库来编写 XML.)

(b) Writing applications should be free to use whatever lexical conventions they find convenient. (Which means you can use any respectable serialization library to write the XML.)

属性的单引号或双引号的选择是这些无关紧要的词汇细节之一.

The choice of single or double quotes for attributes is one of these insignificant lexical details.

我们在 StackOverflow 上看到很多问题是由于人们不遵守这些规则造成的.

We see a LOT of problems on StackOverflow caused by people not following these rules.

您还没有解释为什么需要生成单引号.也许 XML 正在被不遵循规则 (a) 的应用程序使用.在这种情况下,您应该修复它!如果你不能修复它,那么你的成本就会大大增加,事实上,使用 XML 毫无意义.

You haven't explained WHY you need to generate single quotes. Perhaps the XML is being consumed by an application that doesn't follow rule (a). In that case you should fix it! If you can't fix it, then you are vastly increasing your costs, in fact, there's very little point in using XML at all.

有时,当我们问这个问题时,我们会得到因为客户坚持要那样做"的答案.作为专业人士,我们的工作是教育我们的客户:他们是在买单,将钱花在糟糕的工程上是不负责任的,即使他们说他们想要的也是如此.

Sometimes when we ask this question we get the answer "because by client insists on it being that way". It's our job as professionals to educate our clients: they're paying the bills and it's irresponsible to spend their money on bad engineering even if that's what they said they wanted.

这篇关于如何在没有 java 双引号的情况下发送 Jaxb XmlAttribute 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆