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

查看:162
本文介绍了如何在没有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变量,所以我可以在之后添加其他内容实体。问题是如果我用值初始化变量,我将得到String双引号。例如

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;

这样可以正常工作但是如果变量没有初始化那么它就不会添加任何东西而且如果写的话字符串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’="">

所以我无法摆脱来自我的String的报价。

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

我正在考虑硬编码第一个Entity标签并将最后一个关闭它但不能找到任何东西。

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的另一个选项,即使String没有初始化。我在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天全站免登陆