pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何處理帶有 MixedContent 數(shù)據(jù)的 JAXB ComplexType?

How to deal with JAXB ComplexType with MixedContent data?(如何處理帶有 MixedContent 數(shù)據(jù)的 JAXB ComplexType?)
本文介紹了如何處理帶有 MixedContent 數(shù)據(jù)的 JAXB ComplexType?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我得到了這個(gè) XML 結(jié)構(gòu):

<Tax>
  <Money currency="USD">0.00</Money>
  <Description xml:lang="en">
     17.5% Non-Recoverable
    <ShortName>vatspecial</ShortName>
  </Description>
</Tax>

注意 Description 節(jié)點(diǎn)有 MixedContent (由文本和 XML 組成) 這是 XSD 部分關(guān)于 Description 節(jié)點(diǎn):

Notice that Description node has MixedContent (composed with text and XML) and this is the XSD part regarding Description node:

<xsd:complexType name="TaxDescriptionType">
  <xsd:sequence>
    <xsd:element name="ShortName" type="xsd:string" />
  </xsd:sequence>
  <xsd:attribute ref="xml:lang" />
</xsd:complexType>

此時(shí)一切正常,XJC 輸出與 TaxDescriptionType 類(lèi)似的生成類(lèi):

Everything is ok at this point, XJC outputs the generated classes like this one regarding TaxDescriptionType:

package org.com.project;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 * <p>Java class for TaxDescriptionType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="TaxDescriptionType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="ShortName" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *       &lt;attribute ref="{http://www.w3.org/XML/1998/namespace}lang"/>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TaxDescriptionType", propOrder = {
    "shortName"
})
public class TaxDescriptionType {

    @XmlElement(name = "ShortName", required = true)
    protected String shortName;
    @XmlAttribute(name = "lang", namespace = "http://www.w3.org/XML/1998/namespace")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String lang;

    /**
     * Gets the value of the shortName property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getShortName() {
        return shortName;
    }

    /**
     * Sets the value of the shortName property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setShortName(String value) {
        this.shortName = value;
    }

    /**
     * Gets the value of the lang property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getLang() {
        return lang;
    }

    /**
     * Sets the value of the lang property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setLang(String value) {
        this.lang = value;
    }

}

然后,通過(guò)上面的 class 我可以使用以下元素:

Then, with the above class I am able to work around with the elements like this:

taxDescriptionType.setLang("en");
taxDescriptionType.setShortName("vatspecial");
/* missing value: 17.5% Non-Recoverable */

但問(wèn)題是我找不到 getset 17.5% Non-Recoverable 文本的方法來(lái)自上述 XML 示例的 MixedContent-ComplexType.

But the problem is that I can't found a way to get or set the 17.5% Non-Recoverable text of the MixedContent-ComplexType from the above XML example.

這是我嘗試過(guò)的,但它不起作用:

  • 像這樣使用 mixed="true" 屬性:

<xsd:complexType name="TaxDescriptionType" mixed="true">

(我認(rèn)為XJC忽略了最后一個(gè)屬性)

做了一些研究,我發(fā)現(xiàn)了這個(gè):

JAXB XJC 編譯器忽略 XML 上的混合 = true架構(gòu)文檔

但我不確定這是否是解決此問(wèn)題的方法.其中一個(gè)答案說(shuō)這是一個(gè)錯(cuò)誤,而另一個(gè)答案顯示了將 MixedContent 轉(zhuǎn)換為 List 的代碼,也許下一個(gè)情況將是如何處理:

But I am not sure if this is the way to solve this. One of the answers said that this is a bug and in the other one shows a code that transforms the MixedContent into a List<Serializable> and maybe the next situation will be about how to deal with this:

taxDescriptionType.getContent().add(Serializable element);

(而且我真的不知道如何處理 Serializable 元素)

推薦答案

正如你提到的,你需要添加 mixed 屬性來(lái)表明你的類(lèi)型支持混合內(nèi)容.如果沒(méi)有指定,您的 XML 內(nèi)容無(wú)效:

As you mentioned you need to add the mixed attribute to indicate that your type supports mixed content. Without this specified your XML content is invalid:

<xsd:complexType name="TaxDescriptionType" mixed="true">
    <xsd:sequence>
        <xsd:element name="ShortName" type="xsd:string" />
    </xsd:sequence>
    <xsd:attribute ref="xml:lang" />
</xsd:complexType>

生成的 TaxDescriptionType 類(lèi)將具有以下屬性.本質(zhì)上,這意味著所有非屬性?xún)?nèi)容都將存儲(chǔ)在 List 中.這是必要的,因?yàn)槟枰环N機(jī)制來(lái)指示文本節(jié)點(diǎn)在元素內(nèi)容中的位置.

The generated TaxDescriptionType class will have the following property. Essentially this means that all of the non-attribute content will be stored in a List. This is necessary because you need a mechanism that indicates where the text nodes are wrt the element content.

@XmlElementRef(name = "ShortName", namespace = "http://www.example.org/schema", type = JAXBElement.class)
@XmlMixed
protected List<Serializable> content;

您將使用 String(表示文本節(jié)點(diǎn))和 JAXBElement(表示元素內(nèi)容)的實(shí)例填充此列表.

You will populate this list with instances of String (representing text nodes) and JAXBElement (representing element content).

交替

混合內(nèi)容通常會(huì)使生活變得比實(shí)際需要的更加復(fù)雜.如果可能的話(huà),我會(huì)推薦一個(gè)替代的 XML 表示.

Mixed content generally makes life more complicated than it needs to be. If possible I would recommend an alternate XML representation.

<Tax>
  <Money currency="USD">0.00</Money>
  <Description xml:lang="en" ShortName="vatspecial">
    17.5% Non-Recoverable
  </Description>
</Tax>

或者

<Tax>
  <Money currency="USD">0.00</Money>
  <Description xml:lang="en">
    <LongName>17.5% Non-Recoverable</LongName>
    <ShortName>vatspecial</ShortName>
  </Description>
</Tax>

這篇關(guān)于如何處理帶有 MixedContent 數(shù)據(jù)的 JAXB ComplexType?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周?chē)h(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線(xiàn)程逐塊處理文件)
主站蜘蛛池模板: 【法利莱住人集装箱厂家】—活动集装箱房,集装箱租赁_大品牌,更放心 | 捷码低代码平台 - 3D数字孪生_大数据可视化开发平台「免费体验」 | 工业PH计|工业ph酸度计|在线PH计价格-合肥卓尔仪器仪表有限公司 济南画室培训-美术高考培训-山东艺霖艺术培训画室 | 山东柳店新能源科技有限公司| 生物颗粒燃烧机-生物质燃烧机-热风炉-生物颗粒蒸汽发生器-丽水市久凯能源设备有限公司 | 水厂自动化|污水处理中控系统|水利信息化|智慧水务|智慧农业-山东德艾自动化科技有限公司 | 智能型高压核相仪-自动开口闪点测试仪-QJ41A电雷管测试仪|上海妙定 | 上海璟文空运首页_一级航空货运代理公司_机场快递当日达 | 壹车网 | 第一时间提供新车_资讯_报价_图片_排行! | 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | 南京种植牙医院【官方挂号】_南京治疗种植牙医院那个好_南京看种植牙哪里好_南京茀莱堡口腔医院 尼龙PA610树脂,尼龙PA612树脂,尼龙PA1010树脂,透明尼龙-谷骐科技【官网】 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 鹤壁创新仪器公司-全自动量热仪,定硫仪,煤炭测硫仪,灰熔点测定仪,快速自动测氢仪,工业分析仪,煤质化验仪器 | BOE画框屏-触摸一体机-触控查询一体机-触摸屏一体机价格-厂家直销-触发电子 | 亮化工程,亮化设计,城市亮化工程,亮化资质合作,长沙亮化照明,杰奥思【官网】 | 乐之康护 - 专业护工服务平台,提供医院陪护-居家照护-居家康复 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 手持气象站_便携式气象站_农业气象站_负氧离子监测站-山东万象环境 | DNA亲子鉴定_DNA基因检测中心官方预约平台-严选好基因网 | 会议会展活动拍摄_年会庆典演出跟拍_摄影摄像直播-艾木传媒 | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 烟台金蝶财务软件,烟台网站建设,烟台网络推广 | 空心明胶胶囊|植物胶囊|清真胶囊|浙江绿键胶囊有限公司欢迎您! | 原子吸收设备-国产分光光度计-光谱分光光度计-上海光谱仪器有限公司 | 选矿设备-新型重选设备-金属矿尾矿重选-青州冠诚重工机械有限公司 | 铸铝门厂家,别墅大门庭院大门,别墅铸铝门铜门[十大品牌厂家]军强门业 | 铝合金重力铸造_铝合金翻砂铸造_铝铸件厂家-东莞市铝得旺五金制品有限公司 | 安徽控制器-合肥船用空调控制器-合肥家电控制器-合肥迅驰电子厂 安徽净化板_合肥岩棉板厂家_玻镁板厂家_安徽科艺美洁净科技有限公司 | 元拓建材集团官方网站| 微信聊天记录恢复_手机短信删除怎么恢复_通讯录恢复软件下载-快易数据恢复 | 福建自考_福建自学考试网| 衬塑管道_衬四氟管道厂家-淄博恒固化工设备有限公司 | 彭世修脚_修脚加盟_彭世修脚加盟_彭世足疗加盟_足疗加盟连锁_彭世修脚技术培训_彭世足疗 | 医用空气消毒机-医用管路消毒机-工作服消毒柜-成都三康王 | 国标白水泥,高标号白水泥,白水泥厂家-淄博华雪建材有限公司 | 破碎机锤头_耐磨锤头_合金锤头-鼎成机械一站式耐磨铸件定制服务 微型驱动系统解决方案-深圳市兆威机电股份有限公司 | 井式炉-台车式回火炉-丹阳市电炉厂有限公司| 电车线(用于供电给电车的输电线路)-百科| 楼承板-钢筋楼承板-闭口楼承板-无锡优贝斯楼承板厂 | 婚博会2024时间表_婚博会门票领取_婚博会地址-婚博会官网 | 不锈钢复合板厂家_钛钢复合板批发_铜铝复合板供应-威海泓方金属复合材料股份有限公司 |