<!ELEMENT data (rec)*>
<!ELEMENT rec EMPTY>
<!ATTLIST rec a CDATA #REQUIRED>
<!ATTLIST rec b CDATA #REQUIRED>
<!DOCTYPE data SYSTEM "test.dtd">
<data>
<rec>
<a>1</a>
<b>2</b>
</rec>
<rec>
<a>3</a>
<b>4</b>
</rec>
</data>
<!DOCTYPE data SYSTEM "test.dtd">
<data>
<rec a="1" b="2"/>
<rec a="3" b="4"/>
</data>
<!DOCTYPE data SYSTEM "test.dtd">
<data>
<rec a="1" b="2"/>
<rec a="3" b="4.5"/>
</data>
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class ValidateDTD {
public static void testDTD(final String xmlfnm, final String dtdfnm) throws SAXException, IOException, ParserConfigurationException {
System.out.printf("Validating %s against DTD %s\n", xmlfnm, dtdfnm);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException ex) throws SAXException {
System.out.println(ex.getMessage());
}
public void fatalError(SAXParseException ex) throws SAXException {
System.out.println(ex.getMessage());
}
public void warning(SAXParseException ex) throws SAXException {
System.out.println(ex.getMessage());
}
});
db.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(dtdfnm);
}
});
Document doc = db.parse(new File(xmlfnm));
}
public static void main(String[] args) throws Exception {
testDTD("test1.xml", "test.dtd");
testDTD("test2.xml", "test.dtd");
testDTD("test3.xml", "test.dtd");
}
}
Validating test1.xml against DTD test.dtd
Attribute "a" is required and must be specified for element type "rec".
Attribute "b" is required and must be specified for element type "rec".
Element type "a" must be declared.
Element type "b" must be declared.
The content of element type "rec" must match "EMPTY".
Attribute "a" is required and must be specified for element type "rec".
Attribute "b" is required and must be specified for element type "rec".
Element type "a" must be declared.
Element type "b" must be declared.
The content of element type "rec" must match "EMPTY".
Validating test2.xml against DTD test.dtd
Validating test3.xml against DTD test.dtd
<xsd:schema xmlns:xsd="http://www.w3.org/ (...)
xmlns:test="http://arne/ (...)
targetNamespace="http://arne/ (...)
elementFormDefault="qualified">
<xsd:complexType name="recType">
<xsd:attribute name="a" type="xsd:nonNegativeInteger"/>
<xsd:attribute name="b" type="xsd:nonNegativeInteger"/>
</xsd:complexType>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="rec" type="test:recType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<data xmlns="http://arne/ (...)
<rec>
<a>1</a>
<b>2</b>
</rec>
<rec>
<a>3</a>
<b>4</b>
</rec>
</data>
<data xmlns="http://arne/ (...)
<rec a="1" b="2"/>
<rec a="3" b="4"/>
</data>
<data xmlns="http://arne/ (...)
<rec a="1" b="2"/>
<rec a="3" b="4.5"/>
</data>
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public class ValidateSchema {
private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/ (...)
private static final String W3C_XML_SCHEMA = "http://www.w3.org/ (...)
private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/ (...)
public static void testSchema(final String xmlfnm, final String xsdfnm) throws SAXException, IOException, ParserConfigurationException {
System.out.printf("Validating %s against schema %s\n", xmlfnm, xsdfnm);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
SAXParser saxParser = factory.newSAXParser();
saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
saxParser.setProperty(JAXP_SCHEMA_SOURCE, new File(xsdfnm));
saxParser.parse(new File(xmlfnm), new DefaultHandler() {
@Override
public void error(SAXParseException ex) {
System.out.println(ex.getMessage());
}
@Override
public void warning(SAXParseException ex) {
System.out.println(ex.getMessage());
}
});
}
public static void main(String[] args) throws Exception {
testSchema("test4.xml", "test.xsd");
testSchema("test5.xml", "test.xsd");
testSchema("test6.xml", "test.xsd");
}
}
Validating test4.xml against schema test.xsd
cvc-complex-type.2.1: Element 'rec' must have no character or element information item [children], because the type's content type is empty.
cvc-complex-type.2.1: Element 'rec' must have no character or element information item [children], because the type's content type is empty.
Validating test5.xml against schema test.xsd
Validating test6.xml against schema test.xsd
cvc-datatype-valid.1.2.1: '4.5' is not a valid value for 'integer'.
cvc-attribute.3: The value '4.5' of attribute 'b' on element 'rec' is not valid with respect to its type, 'nonNegativeInteger'.
Det er ikke al it-udstyr, som er det rene guld. Her er nogle af læsernes skrækhistorier.
Motorola har begået endnu en smartphone med lækkert design og potentiale til at være blandt de bedste. Men den når ikke i mål. Se her hvorfor.
Hej Vi har mange åbne spørgsmål på Eksperten. Vi ville gerne tvangslukke dem - så et spørgsmål efter f.eks. 6 måneder lukkes. Men der er et par uklarheder som ville være gode at få lidt input til:...
Læs her, hvordan du dropper Facebook og i stedet anvender nogle brugervenlige alternativer, så du stadig kan være social på nettet.
Her har du de mest banebrydende håndholdte computere gennem alle tider.