安卓(Android) 怎么根据schema检查xml文件

2020年2月13日20:31:40 评论 855字阅读2分51秒

1.简介

安卓(Android) 怎么根据schema检查xml文件?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import java.io.File;
import java.net.URL;
import java.util.HashMap;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

public class Main {
    
    static public void schemaCheck(File fileToCheck, URL schemURL)
            throws Exception {

        final String W3C_SCHEMA_SPEC = "http:
        HashMap<String, Schema> MetsSchema = null;

        if (MetsSchema == null) {
            MetsSchema = new HashMap();
        }

        if (MetsSchema.get(schemURL.toString()) == null) {
            
            SchemaFactory factory = SchemaFactory
                    .newInstance(W3C_SCHEMA_SPEC);

            
            MetsSchema
                    .put(schemURL.toString(), factory.newSchema(schemURL));
        }

        
        Validator validator = MetsSchema.get(schemURL.toString())
                .newValidator();

        
        Source source = new StreamSource(fileToCheck);

        
        validator.validate(source);
    }
}

 
继续阅读
编程十万个怎么办
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: