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

在 Java 中針對(duì) XSD 驗(yàn)證 XML/獲取 schemaLocation

Validate an XML against an XSD in Java / Getting a hold of the schemaLocation(在 Java 中針對(duì) XSD 驗(yàn)證 XML/獲取 schemaLocation)
本文介紹了在 Java 中針對(duì) XSD 驗(yàn)證 XML/獲取 schemaLocation的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

如何在 Java 中使用 XSD 驗(yàn)證 XML 文件?我們事先并不知道模式.我希望能夠獲取 schemaLocation、下載 XSD、緩存它,然后執(zhí)行實(shí)際驗(yàn)證.

How can one validate an XML file using an XSD in Java? We don't know the schema in advance. I would like to be able to get the schemaLocation, download the XSD, cache it and then perform the actual validation.

問(wèn)題是,使用 javax.xml.parsers.DocumentBuilder/DocumentBuilderFactory 類我似乎無(wú)法獲得 schemaLocation 提前.這有什么訣竅?我應(yīng)該研究哪些課程?

The problem is, that with javax.xml.parsers.DocumentBuilder/DocumentBuilderFactory classes I can't seem to be able to get a hold of the schemaLocation in advance. What's the trick for this? Which classes should I look into?

也許我可以使用更合適的 API?整個(gè)問(wèn)題是我們需要?jiǎng)討B(tài)驗(yàn)證,而不是(必須)在本地?fù)碛?XSD.

Perhaps there's a more suitable API I can use? The whole problem is that we need to validate dynamically, without (necessarily) having the XSDs locally.

如何獲取 XSD 文件中定義的 schemaLocation 的 URL?

How could one get a hold of the URL of schemaLocation defined in the XSD file?

我知道您可以設(shè)置功能/屬性,但那是另一回事.我需要先從 XSD 中獲取 schemaLocation.

I know you can set features/attributes, but that's a different thing. I need to get the schemaLocation from the XSD first.

請(qǐng)指教!

推薦答案

鑒于您使用的是 Xerces(或 JDK 默認(rèn)),您是否嘗試過(guò)在出廠時(shí)將此功能設(shè)置為 true:http://apache.org/xml/features/validation/schema.您還可以使用有關(guān)架構(gòu)的其他功能:http://xerces.apache.org/xerces2-j/features.html

Given that you are using Xerces (or JDK default), have you tried setting this feature to true on the factory: http://apache.org/xml/features/validation/schema. There are other features that you can play with regarding schemas: http://xerces.apache.org/xerces2-j/features.html

更新 2(用于緩存):

UPDATE 2 (for caching):

實(shí)現(xiàn)一個(gè) org.w3c.dom.ls.LSResourceResolver 并使用 setResourceResolver 方法在 SchemaFactory 上設(shè)置它.這個(gè)解析器要么從緩存中獲取架構(gòu),要么從位置所指的任何地方獲取它.

Implement a org.w3c.dom.ls.LSResourceResolver and set this on the SchemaFactory using the setResourceResolver method. This resolver would either get the schema from cache or fetch it from wherever the location refers to.

更新 3:

LSResourceresolver 示例(我認(rèn)為這對(duì)您來(lái)說(shuō)是一個(gè)很好的起點(diǎn)):

LSResourceresolver example (which I think will be a good starting point for you):

/**
 * Resolves resources from a base URL
 */
public class URLBasedResourceResolver implements LSResourceResolver {

private static final Logger log = LoggerFactory
        .getLogger(URLBasedResourceResolver.class);

private final URI base;

private final Map<URI, String> nsmap;

public URLBasedResourceResolver(URL base, Map<URI, String> nsmap)
        throws URISyntaxException {
    super();
    this.base = base.toURI();
    this.nsmap = nsmap;
}

@Override
public LSInput resolveResource(String type, String namespaceURI,
        String publicId, String systemId, String baseURI) {
    if (log.isDebugEnabled()) {
        String msg = String
                .format("Resolve: type=%s, ns=%s, publicId=%s, systemId=%s, baseUri=%s.",
                        type, namespaceURI, publicId, systemId, baseURI);
        log.debug(msg);
    }
    if (type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
        if (namespaceURI != null) {
            try {
                URI ns = new URI(namespaceURI);
                if (nsmap.containsKey(ns))
                    return new MyLSInput(base.resolve(nsmap.get(ns)));
            } catch (URISyntaxException e) {
                // ok
            }
        }
    }
    return null;
}

}

MyLSInput 的實(shí)現(xiàn)真的很無(wú)聊:

The implementation of MyLSInput is really boring:

class MyLSInput implements LSInput {

private final URI url;

public MyLSInput(URI url) {
    super();
    this.url = url;
}

@Override
public Reader getCharacterStream() {
    return null;
}

@Override
public void setCharacterStream(Reader characterStream) {

}

@Override
public InputStream getByteStream() {
    return null;
}

@Override
public void setByteStream(InputStream byteStream) {

}

@Override
public String getStringData() {
    return null;
}

@Override
public void setStringData(String stringData) {

}

@Override
public String getSystemId() {
    return url.toASCIIString();
}

@Override
public void setSystemId(String systemId) {
}

@Override
public String getPublicId() {
    return null;
}

@Override
public void setPublicId(String publicId) {
}

@Override
public String getBaseURI() {
    return null;
}

@Override
public void setBaseURI(String baseURI) {

}

@Override
public String getEncoding() {
    return null;
}

@Override
public void setEncoding(String encoding) {

}

@Override
public boolean getCertifiedText() {
    return false;
}

@Override
public void setCertifiedText(boolean certifiedText) {

}

}

這篇關(guān)于在 Java 中針對(duì) XSD 驗(yàn)證 XML/獲取 schemaLocation的文章就介紹到這了,希望我們推薦的答案對(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(使用線程逐塊處理文件)
主站蜘蛛池模板: 华东师范大学在职研究生招生网_在职研究生招生联展网 | LOGO设计_品牌设计_VI设计 - 特创易| 我爱古诗词_古诗词名句赏析学习平台 | 洗地机-全自动/手推式洗地机-扫地车厂家_扬子清洁设备 | 南溪在线-南溪招聘找工作、找房子、找对象,南溪综合生活信息门户! | 锌合金压铸-铝合金压铸厂-压铸模具-冷挤压-誉格精密压铸 | 万烁建筑设计院-建筑设计公司加盟,设计院加盟分公司,市政设计加盟 | 欧洲MV日韩MV国产_人妻无码一区二区三区免费_少妇被 到高潮喷出白浆av_精品少妇自慰到喷水AV网站 | 创客匠人-让IP变现不走弯路 | 西安标准厂房_陕西工业厂房_西咸新区独栋厂房_长信科技产业园官方网站 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 皮带机_移动皮带机_大倾角皮带机_皮带机厂家 - 新乡市国盛机械设备有限公司 | 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 掺铥光纤放大器-C/L波段光纤放大器-小信号光纤放大器-合肥脉锐光电技术有限公司 | 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 电动葫芦|手拉葫芦|环链电动葫芦|微型电动葫芦-北京市凌鹰起重机械有限公司 | 二手色谱仪器,十万分之一分析天平,蒸发光检测器,电位滴定仪-湖北捷岛科学仪器有限公司 | 厦门ISO认证|厦门ISO9001认证|厦门ISO14001认证|厦门ISO45001认证-艾索咨询专注ISO认证行业 | 注浆压力变送器-高温熔体传感器-矿用压力传感器|ZHYQ朝辉 | 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 生态板-实木生态板-生态板厂家-源木原作生态板品牌-深圳市方舟木业有限公司 | 【孔氏陶粒】建筑回填陶粒-南京/合肥/武汉/郑州/重庆/成都/杭州陶粒厂家 | 槽钢冲孔机,槽钢三面冲,带钢冲孔机-山东兴田阳光智能装备股份有限公司 | 国际线缆连接网 - 连接器_线缆线束加工行业门户网站 | 广东护栏厂家-广州护栏网厂家-广东省安麦斯交通设施有限公司 | 全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司 | 注浆压力变送器-高温熔体传感器-矿用压力传感器|ZHYQ朝辉 | 乳化沥青设备_改性沥青设备_沥青加温罐_德州市昊通路桥工程有限公司 | 考勤系统_人事考勤管理系统_本地部署BS考勤系统_考勤软件_天时考勤管理专家 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | UV固化机_UVLED光固化机_UV干燥机生产厂家-上海冠顶公司专业生产UV固化机设备 | 蜜蜂职场文库_职场求职面试实用的范文资料大全 | 铁艺,仿竹,竹节,护栏,围栏,篱笆,栅栏,栏杆,护栏网,网围栏,厂家 - 河北稳重金属丝网制品有限公司 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 烟气换热器_GGH烟气换热器_空气预热器_高温气气换热器-青岛康景辉 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | 杭州可当科技有限公司—流量卡_随身WiFi_AI摄像头一站式解决方案 | 环比机械| 东风体检车厂家_公共卫生体检车_医院体检车_移动体检车-锦沅科贸 | 时代北利离心机,实验室离心机,医用离心机,低速离心机DT5-2,美国SKC采样泵-上海京工实业有限公司 工业电炉,台车式电炉_厂家-淄博申华工业电炉有限公司 |