java程序验证xml和xsd中的枚举

时间:2014-06-05 09:49:17

标签: java xml xsd

我需要检测枚举中的错误

这是我用枚举验证XML和XSD的代码,但是我遇到了很多错误。有人可以给我一些关于错误消息的反馈(由java程序打印)吗?

这是我要验证的文件

<?xml version="1.0" encoding="UTF-8"?> 
<person name="avinash" age="27" gender="male"> 
    <address type="email">xgjjkk@gmail.com</address> 
    <address type="phone">khkhlll</address> 
    <address type="company">jgjhgjhgk</address>  
</person> 

这是我的架构

<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/persondetails" xmlns:tns="http://www.example.org/persondetails" elementFormDefault="qualified"> 
    <element name="person"> 
        <complexType> 
            <sequence> 
                <element name="address"> 
                    <complexType> 
                        <attribute name="type"> 
                            <simpleType> 
                                <restriction base="string"> 
                                    <enumeration value="email"></enumeration> 
                                    <enumeration value="phone"></enumeration> 
                                    <enumeration value="company"></enumeration> 
                                </restriction> 
                            </simpleType> 
                        </attribute> 
                    </complexType> 
                </element> 
            </sequence> 
            <attribute name="name" type="string"></attribute>  
            <attribute name="age" type="int"></attribute>  
            <attribute name="gender" type="string"></attribute> 
        </complexType> 
    </element> 
</schema> 

Java程序

package com.pack.group;

import javax.xml.XMLConstants; 
import javax.xml.transform.stream.StreamSource; 
import javax.xml.validation.Schema; 
import javax.xml.validation.SchemaFactory; 
import javax.xml.validation.Validator; 

public class Error { 

    public static void main(String[] args) throws Exception { 
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
        Schema schema = factory.newSchema(new StreamSource("D:/project/enum1.xsd")); 
        Validator validator = schema.newValidator(); 
        validator.validate(new StreamSource("D:/project/enum.xml")); 
    } 
} 

这些是我得到的错误

  

线程中的异常&#34; main&#34; org.xml.sax.SAXParseException:cvc-elt.1:   找不到元素&#39;的声明。在   com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)   在   com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)   在   com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)   在   com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)   在   com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1916)   在   com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:705)

1 个答案:

答案 0 :(得分:0)

您的架构将address声明为 empty 元素(它只能包含属性),因此您的实例不会验证。

如果您希望允许address元素中的简单文本内容,则需要以不同方式声明它,将属性声明作为元素简单内容的扩展:

<element name="address"> 
    <complexType> 
        <simpleContent>
            <extension base="string">
                <attribute name="type"> 
                    <simpleType> 
                        <restriction base="string"> 
                            <enumeration value="email"></enumeration> 
                            <enumeration value="phone"></enumeration> 
                            <enumeration value="company"></enumeration> 
                        </restriction> 
                    </simpleType> 
                </attribute> 
            </extension>
        </simpleContent>
    </complexType> 
</element> 

您的实例仍然无法验证,因为您只允许一个address元素。您可以使用maxOccurs="unbounded"(或某些固定值,例如maxOccurs="3")来更改它:

<element name="person"> 
    <complexType> 
        <sequence> 
            <element name="address" maxOccurs="unbounded"> 
....