問題描述
所以我在嘗試使用 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 數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!