Avatar billede _henrik Nybegynder
05. februar 2007 - 22:38 Der er 1 kommentar

Validering af XML dokument

Jeg har skrevet nedenstående kode til at validere en xml-fil. Men når jeg prøver at validere filen
får jeg følgende fejl
ERROR:  'cvc-elt.1: Cannot find the declaration of element 'Limits'.'



import java.io.*;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;

public class XMLValidator {

  private Validator validator;
  private final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";

  public XMLValidator(InputStream[] schemas) throws SAXException {
    SchemaFactory factory = SchemaFactory.newInstance(W3C_XML_SCHEMA);
    Source[] sources = new Source[schemas.length];

    for (int i = 0; i < schemas.length; i++) {
      sources[i] = new StreamSource(schemas[i]);
    }
 
    Schema schema = factory.newSchema(sources);
    validator = schema.newValidator();
  }

  public void validate(File xmlFile) throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(xmlFile);
    DOMSource source = new DOMSource(doc);
    DOMResult result = new DOMResult();
    validator.validate(source, result);
  }
}

-------------

XML filen:

<?xml version="1.0" encoding="UTF-8"?>
<Limits xmlns="http://www.eksperten.dk/2007" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eksperten.dk/2007 Limits.xsd">
  <Device orderNo="080z0121" version="1-0x">
    <component id="1">
      <variable id="2" xsi:type="variableInt16">
        <min>0</min>
        <max>256</max>
      </variable>
      <variable id="1" xsi:type="variableInt16">
        <min>2</min>
        <max>30000</max>
      </variable>
    </component>
  </Device>
</Limits>

---------

Limits.xsd

<?xml version="1.0" encoding="utf-8"?>

<xs:schema attributeFormDefault="unqualified"
          elementFormDefault="qualified"
          targetNamespace="http://www.danfoss.com/2006/ak2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:ak2="http://www.eksperten.dk/2007 "
          xsi:schemaLocation="http://www.eksperten.dk/2007 ak2BasicTypes.xsd">
  <xs:include schemaLocation="ak2BasicTypes.xsd"/>

 
  <xs:element name="Limits">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Device" minOccurs="1" maxOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="component" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="variable" type="ak2:variableBase" minOccurs="0" maxOccurs="unbounded"/>
                  </xs:sequence>
                  <xs:attribute name="id" type="ak2:uint16" use="required" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="orderNo" type="xs:string" use="required" />
            <xs:attribute name="version" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>


  <xs:complexType name="variableBase" abstract="true">
    <xs:attribute name="id" type="ak2:uint16" use="required"/>
  </xs:complexType>
 
 
  <xs:complexType name="variableUint8">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:uint8"/>
          <xs:element name="max" type="ak2:uint8"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="variableUint16">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:uint16"/>
          <xs:element name="max" type="ak2:uint16"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="variableUint32">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:uint32"/>
          <xs:element name="max" type="ak2:uint32"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="variableInt8">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:int8"/>
          <xs:element name="max" type="ak2:int8"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="variableInt16">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:int16"/>
          <xs:element name="max" type="ak2:int16"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="variableInt32">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:int32"/>
          <xs:element name="max" type="ak2:int32"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>
-----------------

BasicTypes.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
          targetNamespace="http://www.eksperten.com/2007" 
          attributeFormDefault="unqualified"
          elementFormDefault="qualified">

  <xs:simpleType name="bool">
    <xs:restriction base="xs:boolean">
    </xs:restriction>
  </xs:simpleType>
 
  <xs:simpleType name="uint8">
    <xs:restriction base="xs:unsignedByte"/>
  </xs:simpleType>

  <xs:simpleType name="uint16">
    <xs:restriction base="xs:unsignedShort"/>
  </xs:simpleType>

  <xs:simpleType name="uint32">
    <xs:restriction base="xs:unsignedInt"/>
  </xs:simpleType>

  <xs:simpleType name="int8">
    <xs:restriction base="xs:byte"/>
  </xs:simpleType>

  <xs:simpleType name="int16">
    <xs:restriction base="xs:short"/>
  </xs:simpleType>

  <xs:simpleType name="int32">
    <xs:restriction base="xs:int"/>
  </xs:simpleType>

  <xs:simpleType name="real32">
    <xs:restriction base="xs:decimal">
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="string16">
    <xs:restriction base="xs:string">
      <xs:maxLength value="16"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="string20">
    <xs:restriction base="xs:string">
      <xs:maxLength value="20"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="string32">
    <xs:restriction base="xs:string">
      <xs:maxLength value="32"/>
    </xs:restriction>
  </xs:simpleType>
 
</xs:schema>
Avatar billede _henrik Nybegynder
05. februar 2007 - 22:40 #1
ups

Limits.xsd

<?xml version="1.0" encoding="utf-8"?>

<xs:schema attributeFormDefault="unqualified"
          elementFormDefault="qualified"
          targetNamespace="http://www.eksperten.dk/2007"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:ak2="http://www.eksperten.dk/2007 "
          xsi:schemaLocation="http://www.eksperten.dk/2007 ak2BasicTypes.xsd">
  <xs:include schemaLocation="ak2BasicTypes.xsd"/>


  <xs:element name="Limits">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Device" minOccurs="1" maxOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="component" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="variable" type="ak2:variableBase" minOccurs="0" maxOccurs="unbounded"/>
                  </xs:sequence>
                  <xs:attribute name="id" type="ak2:uint16" use="required" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="orderNo" type="xs:string" use="required" />
            <xs:attribute name="version" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>


  <xs:complexType name="variableBase" abstract="true">
    <xs:attribute name="id" type="ak2:uint16" use="required"/>
  </xs:complexType>


  <xs:complexType name="variableUint8">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:uint8"/>
          <xs:element name="max" type="ak2:uint8"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="variableUint16">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:uint16"/>
          <xs:element name="max" type="ak2:uint16"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="variableUint32">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:uint32"/>
          <xs:element name="max" type="ak2:uint32"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="variableInt8">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:int8"/>
          <xs:element name="max" type="ak2:int8"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="variableInt16">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:int16"/>
          <xs:element name="max" type="ak2:int16"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="variableInt32">
    <xs:complexContent>
      <xs:extension base="ak2:variableBase">
        <xs:sequence>
          <xs:element name="min" type="ak2:int32"/>
          <xs:element name="max" type="ak2:int32"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester