没有命名空间的xml的Java xsd验证 [英] Java xsd validation of xml without namespace

查看:88
本文介绍了没有命名空间的xml的Java xsd验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想针对XSD架构验证XML文件。 XML文件根元素没有任何名称空间或xsi详细信息。它没有属性所以只有< root>

I want to validate an XML file against an XSD schema. The XML files root element does not have any namespace or xsi details. It has no attributes so just <root>.

我尝试了以下代码 http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi。 html 没有运气,因为我收到
cvc-elt.1:找不到元素'root'的声明

I have tried the following code from http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi.html with no luck as I receive cvc-elt.1: Cannot find the declaration of element 'root'

SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

File schemaFile = new File("schema.xsd");

Schema xsdScheme = factory.newSchema(schemaFile);

Validator validator = xsdScheme.newValidator();

Source source = new StreamSource(xmlfile);

validator.validate(source);

xml使用包含的命名空间标题(通过xmlspy添加)验证正常,但我会想到可以在不必手动编辑源文件的情况下声明xml命名空间吗?

The xml validates fine with the namespace headers included etc (added via xmlspy), but I would have thought the xml namespace could be declared without having to manually edit the source file?

编辑和解决方案:

public static void validateAgainstXSD(File file) {

    try {
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        File schemaFile = new File("path/to/xsd");

        Schema xsdScheme = factory.newSchema(schemaFile);

        Validator validator = xsdScheme.newValidator();

        SAXSource source = new SAXSource(
                new NamespaceFilter(XMLReaderFactory.createXMLReader()),
                new InputSource(new FileInputStream(file)));

        validator.validate(source,null);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

protected static class NamespaceFilter extends XMLFilterImpl {

    String requiredNamespace = "namespace";

    public NamespaceFilter(XMLReader parent) {
        super(parent);
    }

    @Override
    public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
        if(!arg0.equals(requiredNamespace)) 
            arg0 = requiredNamespace;
        super.startElement(arg0, arg1, arg2, arg3);
    }       
}


推荐答案

你您需要注意两个单独的问题:

You have two separate concerns you need to take care of:


  1. 声明文档使用的命名空间。

  2. 在文件中放置 xsi:schemaLocation 属性,以提示(!)架构所在的位置。

  1. Declaring the namespace that your document uses.
  2. Putting an xsi:schemaLocation attribute in the file to give a hint (!) where the schema is.

您可以安全地跳过第二部分,因为该位置实际上只是一个提示。你不能跳过第一部分。 XML文件中声明的名称空间与模式匹配。重要的是,

You can safely skip the second part, as the location is really only a hint. You cannot skip the first part. The namespace declared in the XML file is matched against the schema. Important, this:

<xml> ... </xml>

与此不一样

<xml xmlns="urn:foo"> ... </xml>

所以你需要在XML文档中声明你的命名空间,否则它将不符合你的模式和你会收到这个错误。

So you need to declare your namespace in the XML document, otherwise it will not correspond to your schema and you will get this error.

这篇关于没有命名空间的xml的Java xsd验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆