Java 发现以元素';开头的内容无效;说明';。一个';{quot;http://test.com/test/v1.0";:description}';预计
java代码:Java 发现以元素';开头的内容无效;说明';。一个';{quot;http://test.com/test/v1.0";:description}';预计,java,Java,java代码: public static boolean validateXMLSchema(){ try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new File("students.xsd"));
public static boolean validateXMLSchema(){
try {
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File("students.xsd"));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File("students.xml")));
} catch (IOException e){
System.out.println("Exception: "+e.getMessage());
return false;
}catch(SAXException e1){
System.out.println("SAX Exception: "+e1.getMessage());
return false;
}
students.xsd文件
<?xml version = "1.0"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:midi="http://test.com/test/v1.0">
<xs:import namespace="http://test.com/test/v1.0" schemaLocation="midi.xsd"/>
<xs:element name = 'class'>
<xs:complexType>
<xs:sequence>
<xs:element name = 'student' type = 'StudentType' minOccurs = '1'
maxOccurs = '1' />
<xs:element name='rule' type='midi:rule' minOccurs = '1'
maxOccurs = '1' />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name = "StudentType">
<xs:sequence>
<xs:element name = "firstname" type = "xs:string"/>
<xs:element name = "marks" type = "xs:positiveInteger"/>
</xs:sequence>
<xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
</xs:complexType>
</xs:schema>
midi.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://test.com/test/v1.0"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://test.com/test/v1.0">
<xs:complexType name="rule">
<xs:sequence>
<xs:element name="description" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<!-- <xs:element name="rule" type="rule"/>-->
</xs:schema>
和我的xml文件
<?xml version = "1.0"?>
<class>
<student rollno = "593">
<firstname>Jasvir</firstname>
<marks>90</marks>
</student>
<rule >
<description>test</description>
</rule>
</class>
贾斯维尔
90
测试
任何人都可以帮助我
当我执行java代码时,我得到了下面的错误
SAX异常:cvc复杂类型。2.4.a:发现以元素“description”开头的内容无效。应为“{”“:description}”之一。
对…无效