jdom实现schema验证xml文件是否合法
导入jdom1.1版
http://www.jdom.org/dist/binary/
import java.io.FileReader;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
public class XMLValidate {
public void validate(String xml, String schema) {
try {
SAXBuilder builder = new SAXBuilder(true);
//指定约束方式为XML schema
builder.setFeature("http://apache.org/xml/features/validation/schema",true);
//导入schema文件
builder.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",schema);
Document doc = builder.build(new FileReader(xml));
System.out.println("恭喜您,验证通过!!!");
}catch(Exception e) {
System.out.println("验证失败:"+e);
}
}
public static void main(String args[]) {
new XMLValidate().validate("e:\\a.xml","e:\\b.xsd");
}
}
老版本需要用到Xerces
参见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
相关文档:
一、XML只有一个Table的情况
(1)userInfo.xml
<?xml version="1.0" encoding="utf-8" ?>
<UserInfo ......
<
<
小于
>
>
大于
&
&
和号
'
'
单引号
"
"
引号
注释:在 XML 中,只有字符 "<" 和 "&" 确实是非法的。大于号是合法的,但是用实体引用来代替它是一个好习惯。 ......
<?xml version="1.0" encoding="UTF-8"?>
<projects>
<node Country="暗暗啊" Gold="10" Silver="20" Bronze="30"/>
<node Country="白斑病" Gold="30" Silver="20" Bronze="10"/>
<node Country="常常厂" Gold="20" Silver="40" Bronze="60"/>
<node Country="赌东道" Gold="5 ......
*
sql xml 入门:
--by jinjazz
--http://blog.csdn.net/jinjazz
1、xml: 能认识元素、属性和值
2、xpath: 寻址语言,类似wind ......
写XML:
protected void btnSave_Click(object sender, EventArgs e)
{
//权限判断
XmlTextWriter xmlw = new XmlTextWriter(Server.MapPath("~\\") + "FriendLink.xml", Encoding.Ge ......