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

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

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

問題描述

我將 jaxb 用于我的應用程序配置

I am using jaxb for my application configurations

我覺得我在做一些非常不正當的事情,我正在尋找一種不需要實際文件或此交易的方法.

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 中創建一個模式到一個文件中(實際上來自我的類??注釋)2.設置此模式文件以便在我解組時允許真正的驗證

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(...);

你們中有人知道如何驗證 jaxb,而無需在我的計算機中創建架構文件嗎?

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

我是否需要創建一個模式進行驗證,當我通過 JaxbContect.generateSchema 獲得它時它看起來是多余的?

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

你是怎么做到的?

推薦答案

關于上面ekeren的解決方案,在單線程中使用PipedOutputStream/PipedInputStream并不是一個好主意,以免溢出緩沖區導致死鎖.ByteArrayOutputStream/ByteArrayInputStream 有效,但如果您的 JAXB 類生成多個模式(在不同的命名空間中),您需要多個 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.

我最終得到了這個:

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

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

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

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产精品欧美一区二区 | 亚洲在线一区二区 | 久久久91精品国产一区二区三区 | 久久久久久国产精品久久 | 99热在线观看精品 | 色婷婷av久久久久久久 | 国产成人精品一区二区三区四区 | 国产精品免费在线 | 精品在线视频播放 | 亚洲电影中文字幕 | 日韩欧美一区在线 | av三级在线观看 | www.色综合| 亚洲二区在线 | 在线日韩福利 | 中文精品视频 | 精品日韩一区二区 | 自拍偷拍亚洲欧美 | 欧美中文字幕一区二区三区亚洲 | 91精品国产色综合久久不卡98 | 精品久久久久久久久久久久久久 | 亚洲天堂日韩精品 | 国产视频2021 | 一级特黄a大片 | 国产精品一区二区三区四区 | 国产91在线 | 亚洲 | 久久国产精品-久久精品 | 欧美伦理一区 | 欧美一区二区小视频 | 久久这里只有 | 国产精品一区二区在线 | 91麻豆精品国产91久久久久久 | 97色在线视频| 四虎在线视频 | 日日天天 | 成人欧美一区二区三区色青冈 | 特黄小视频| 欧美日韩中文字幕 | 亚洲色图插插插 | www亚洲精品 | 日本精品视频一区二区 |