久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我如何在 jaxb 中解組并在不使用顯式模式文件的

how can i unmarshall in jaxb and enjoy the schema validation without using an explicit schema file(我如何在 jaxb 中解組并在不使用顯式模式文件的情況下享受模式驗(yàn)證)
本文介紹了我如何在 jaxb 中解組并在不使用顯式模式文件的情況下享受模式驗(yàn)證的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我將 jaxb 用于我的應(yīng)用程序配置

I am using jaxb for my application configurations

我覺得我在做一些非常不正當(dāng)?shù)氖虑椋艺趯ふ乙环N不需要實(shí)際文件或此交易的方法.

I feel like I am doing something really crooked and I am looking for a way to not need an actual file or this transaction.

正如你在代碼 I 中看到的:

As you can see in code I:

1.從我的 JaxbContext 中創(chuàng)建一個(gè)模式到一個(gè)文件中(實(shí)際上來自我的類??注釋)2.設(shè)置此模式文件以便在我解組時(shí)允許真正的驗(yàn)證

1.create a schema into a file from my JaxbContext (from my class annotation actually) 2.set this schema file in order to allow true validation when I unmarshal

JAXBContext context = JAXBContext.newInstance(clazz);
Schema mySchema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);
jaxbContext.generateSchema(new MySchemaOutputResolver()); // ultimately creates schemaFile   
Unmarshaller u = m_context.createUnmarshaller();
u.setSchema(mySchema);
u.unmarshal(...);

你們中有人知道如何驗(yàn)證 jaxb,而無需在我的計(jì)算機(jī)中創(chuàng)建架構(gòu)文件嗎?

do any of you know how I can validate jaxb without needing to create a schema file that sits in my computer?

我是否需要?jiǎng)?chuàng)建一個(gè)模式進(jìn)行驗(yàn)證,當(dāng)我通過 JaxbContect.generateSchema 獲得它時(shí)它看起來是多余的?

Do I need to create a schema for validation, it looks redundant when I get it by JaxbContect.generateSchema ?

你是怎么做到的?

推薦答案

關(guān)于上面ekeren的解決方案,在單線程中使用PipedOutputStream/PipedInputStream并不是一個(gè)好主意,以免溢出緩沖區(qū)導(dǎo)致死鎖.ByteArrayOutputStream/ByteArrayInputStream 有效,但如果您的 JAXB 類生成多個(gè)模式(在不同的命名空間中),您需要多個(gè) StreamSource.

Regarding ekeren's solution above, it's not a good idea to use PipedOutputStream/PipedInputStream in a single thread, lest you overflow the buffer and cause a deadlock. ByteArrayOutputStream/ByteArrayInputStream works, but if your JAXB classes generate multiple schemas (in different namespaces) you need multiple StreamSources.

我最終得到了這個(gè):

JAXBContext jc = JAXBContext.newInstance(Something.class);
final List<ByteArrayOutputStream> outs = new ArrayList<ByteArrayOutputStream>();
jc.generateSchema(new SchemaOutputResolver(){
    @Override
    public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        outs.add(out);
        StreamResult streamResult = new StreamResult(out);
        streamResult.setSystemId("");
        return streamResult;
    }});
StreamSource[] sources = new StreamSource[outs.size()];
for (int i=0; i<outs.size(); i++) {
    ByteArrayOutputStream out = outs.get(i);
    // to examine schema: System.out.append(new String(out.toByteArray()));
    sources[i] = new StreamSource(new ByteArrayInputStream(out.toByteArray()),"");
}
SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
m.setSchema(sf.newSchema(sources));
m.marshal(docs, new DefaultHandler());  // performs the schema validation

這篇關(guān)于我如何在 jaxb 中解組并在不使用顯式模式文件的情況下享受模式驗(yàn)證的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(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ù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 91传媒在线观看 | 久久99精品久久久久子伦 | 婷婷福利视频导航 | 国产福利久久 | 精久久久| 人人色视频 | 色www精品视频在线观看 | 一级美国黄色片 | 亚洲乱码国产乱码精品精98午夜 | 精品一区二区三区日本 | 久久毛片 | 国产不卡视频 | 国产精品一区久久久 | 亚洲免费在线观看av | 国产91久久精品一区二区 | 亚洲免费视频在线观看 | 成人在线免费 | 久久国产精品一区二区三区 | 久久久精品 | 久久久久黄色 | 精品国产伦一区二区三区观看说明 | 狠狠操婷婷 | 综合网视频 | 精品三级在线观看 | 日韩视频精品在线 | 丝袜 亚洲 欧美 日韩 综合 | 97人人超碰| 免费看啪啪网站 | 亚洲+变态+欧美+另类+精品 | 久国产精品 | 日本激情视频在线播放 | 欧美日韩视频在线第一区 | www国产成人免费观看视频,深夜成人网 | 伊人av在线播放 | 国产精品a久久久久 | 91最新在线视频 | 久久精品一区二区三区四区 | 日韩av在线播 | 久久久天天 | 一级毛片视频在线观看 | 国产99久久精品一区二区永久免费 |