在线等-关于XML schema验证的问题!(100分)

sunshengx 2004-04-29 08:29:48
求救:
我现在写了一个schema 文件名:cmd-config.xsd
按照上面的schema 还写了一个xml 文件 ->cmd-config.xml
我现在用的是JDOM 请问如何用代码来验证 我的cmd-config.xml 是否
满足cmd-config.xsd定义的格式。
如果不用Jdom 那应该用什么
我已经用JDOM 试了 但是不行 总是出错
各位ggjj帮忙啊
谢谢!!!!!!!
...全文
37 4 点赞 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
bromon 2004-05-09
前几天我做过这个,整理了一些材料放在我blog上:

http://www.blogcn.com/blog/cool/main.asp?uid=bromon&id=1655225
  • 打赏
  • 举报
回复
ln_boy 2004-05-08
http://www.jdom.org/docs/faq.html#a0360


How do I validate against a schema when using JDOM?

JDOM doesn't have it's own parser; it uses standard parsers like Xerces to do the heavy lifting. If you want schema validation make sure you choose a parser that supports schemas. Xerces 2 is a good choice (get it from http://xml.apache.org). You also need to use code JDOM Beta 8 or later.

To specify the parser JDOM uses, you can either configure JAXP appropriately (since JDOM uses JAXP if it's available, see the end of this entry for details) or you can explicitly pass the name of the parser to the SAXBuilder constructor. For Xerces 2 the parser class is org.apache.xerces.parsers.SAXParser. You must also enable parser validation by passing "true" when creating a SAXBuilder.

SAXBuilder builder =
new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);

Next, you tell the parser (Xerces) you want to validate against a schema (or schemas), and you pass the parser information about those schema. Different parsers do this in different ways. In Xerces you do this by setting special 'features' and 'properties' of the parser. JDOM exposes these parser settings with the setFeature() and setProperty() methods on SAXBuilder. These pass-through methods were added after Beta 7, which is why you need Beta 8 or above.

Schemas are enabled by setting the feature "http://apache.org/xml/features/validation/schema" to true.

builder.setFeature(
"http://apache.org/xml/features/validation/schema", true);

Schema locations are given by setting the property "http://apache.org/xml/properties/schema/external-schemaLocation" to a list of whitespace separated name-value pairs. The 'name' is the namespace the schema is associated with, the 'value' is the location of the schema for that namespace. For example:

builder.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation", "http://www.w3.org/2001/12/soap-envelope soap-envelope.xsd" + " " + "http://kevinj.develop.com/weblog/weblog.xsd weblog.xsd");

The above example shows how to validate against multiple schemas -- against the SOAP 1.2 schema where the namespace is http://www.w3.org/2001/12/soap-envelope and the and against a schema for namespace http://kevinj.develop.com/weblog/weblog.xsd. The files describing these schemas are in soap-envelope.xsd and weblog.xsd respectively. You can add as many of these name value pairs as necessary. The values themselves are URLs. The name value pairs follow the meaning given in the Schema recommendation (http://www.w3.org/TR/xmlschema-1/#schema-loc ).

The complete code looks like this:

SAXBuilder builder =
new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);
builder.setFeature(
"http://apache.org/xml/features/validation/schema", true);
builder.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation",
"http://www.w3.org/2001/12/soap-envelope soap-envelope.xsd" + " " +
"http://kevinj.develop.com/weblog/weblog.xsd weblog.xsd");
Document doc = builder.build(xml);

If you want to use JAXP to select the parser, you can skip specifying a class to the SAXBuilder constructor and instead set the system property "javax.xml.parsers.SAXParserFactory" to the value "org.apache.xerces.jaxp.SAXParserFactoryImpl". That tells JAXP to use Xerces' factory to build parsers. If you like, you can specify this property on the command line:

java -Djavax.xml.parsers.SAXParserFactory=
org.apache.xerces.jaxp.SAXParserFactoryImpl ...

(Contributed by Kevin Jones)


  • 打赏
  • 举报
回复
minghuitian 2004-05-06
up
  • 打赏
  • 举报
回复
viano 2004-04-30


帮你顶顶!

正在研究这个!
  • 打赏
  • 举报
回复
相关推荐
发帖
Java EE
加入

6.7w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2004-04-29 08:29
社区公告
暂无公告