以文本方式查看主题 - 计算机科学论坛 (http://bbs.xml.org.cn/index.asp) -- 『 DTD/XML Schema 』 (http://bbs.xml.org.cn/list.asp?boardid=23) ---- 怎样编写java程序验证XML文档是否符合XML Schema? (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=12194) |
-- 作者:grand -- 发布时间:11/20/2004 1:55:00 PM -- 怎样编写java程序验证XML文档是否符合XML Schema? 比如XML 文档: <?xml version="1.0" encoding="UTF-8"?> <!-- edited with XMLSPY v5 rel. 4 U (http://www.xmlspy.com) by rth77 (rth77) --> <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cst="http://schemas.dbs.com/icc/CustomerSvc" xmlns:ems="http://schemas.dbs.com/icc/ems" xmlns:op="http://schemas.dbs.com/icc/CRMSvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ C:/FPS_HOME/cfg/Schema/OpptyAdd/schema/OpptyAdd.xsd"> <Header> <ems:EMSDetail mustUnderstand="false"> <ems:MsgVersion>1.1</ems:MsgVersion> <ems:MsgUID>OOOOOOOOOOOOOOO041112153055064122991</ems:MsgUID> <ems:SvcVersion>1.0</ems:SvcVersion> </ems:EMSDetail> <ems:Trace mustUnderstand="false"> <ems:RqDateTime>2004-11-12T03:30:55+08:00</ems:RqDateTime> <ems:RqClient> <ems:RqClientId>FPS</ems:RqClientId> <ems:RqClientOrg>0001</ems:RqClientOrg> <ems:RqClientCtry>SG</ems:RqClientCtry> </ems:RqClient> <ems:Operator> <ems:OpLoginId/> <ems:OpRole>TELLER</ems:OpRole> </ems:Operator> </ems:Trace> <ems:Security/> </Header> <Body> <op:OpptyAdd> <ems:CommonRq> <ems:ChannelId>00</ems:ChannelId> <ems:EnableSAF>false</ems:EnableSAF> </ems:CommonRq> <ems:RqSysRef>FPS041109134414230204181</ems:RqSysRef> <ems:OrgCode>0001</ems:OrgCode> <op:SalesMethod>WALK IN</op:SalesMethod> <op:DeptProdGrp>CBG-FINANCIAL PLANNING</op:DeptProdGrp> <op:SalesCycle>05</op:SalesCycle> <op:SalesStaffDetl> <op:SalesStaffId>204181</op:SalesStaffId> </op:SalesStaffDetl> <op:ReferralSource>EMPLOYEE</op:ReferralSource> </op:OpptyAdd> </Body> </Envelope> XMLSchema如下: <?xml version="1.0" encoding="UTF-8"?> Description: Add revenue generating item for the Bank to a given contact record |
-- 作者:小懒龙 -- 发布时间:12/4/2004 6:17:00 PM -- 这个。。。一般情况下不用自己写程序来验证的呀。解析器如果支持合法性验证的话,在LoadXML文档的时候就完成验证了。 |
-- 作者:xmzhy -- 发布时间:12/6/2004 11:02:00 AM -- ding |
-- 作者:wxb_nudt -- 发布时间:12/9/2004 7:17:00 PM -- 使用XMLSPY,它会自动为你验证XML schema的合法性,如果不合法,就在你要存储XML文件时报错! |
-- 作者:LeslieZhu -- 发布时间:1/24/2005 3:20:00 PM -- 我想楼主的意思是他的xml文件格式是否符合他定义的schema吧 我也想知道 |
-- 作者:dongdonga -- 发布时间:1/27/2005 1:14:00 PM -- 我也想知道,帮你up! |
-- 作者:yetties -- 发布时间:1/29/2005 11:57:00 AM -- 如果不符合schema,xml文档能正常显示吗? |
-- 作者:maxgong -- 发布时间:10/8/2005 4:24:00 PM -- public boolean validate(InputStream in, InputStream schema, ErrorHandle errorHandle) throws Exception { log.debug("DefaultParser.validate() begin:"); boolean result = false; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); factory.setAttribute(SCHEMA_LANGUAGE, XML_SCHEMA); factory.setAttribute(SCHEMA_SOURCE, schema); DocumentBuilder parser = factory.newDocumentBuilder(); parser.setErrorHandler(errorHandle); parser.parse(in); if (errorHandle.getMsgList().size() == 0) { |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
93.750ms |