udało mi się znaleźć łatwy sposób na sprawdzanie pliku xml tylko teraz nie wiem jak uwzględnić polskie znaki bo
kiedy przekształca string do postaci bitów nie chce mi zamienić polskich znaków,
wyskakuje błąd:
Exception in thread "main" java.lang.Exception: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at Serializacja.SchemaValidator.validate(Serializacja.java:47)
at Serializacja.Serializacja.main(Serializacja.java:58)
Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.StreamValidatorHelper.validate(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Unknown Source)
at Serializacja.SchemaValidator.validate(Serializacja.java:41)
... 1 more
class SchemaValidator{
private static final String XSD_FILE = "./plikxsd2.xsd";
public static boolean validate(String xml) throws Exception{
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//URL resource = this.getClass().getClassLoader().getResource(XSD_FILE);
Schema schema;
try {
schema = factory.newSchema(new File(XSD_FILE));;
} catch (Exception e) {
throw new Exception(e);
}
Validator validator = schema.newValidator();
//jak tu wczytać polskie znaki?
Source source = new StreamSource(new ByteArrayInputStream(xml.getBytes()));
try {
validator.validate(source);
System.out.println("XML Validation: OK");
} catch (SAXException ex) {
System.out.println("XML is not valid "+ex.getMessage());
return false;
} catch (IOException e) {
throw new Exception(e);
}
return true;
}
}
public class Serializacja{
public static void main(String[] args) throws Exception{
//tutaj nie chce mi wczytać polskich znaków
SchemaValidator.validate("<płyty></płyty>");
}
}
plikxsd2.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="płyty" type="xs:string"/>
</xs:schema>
ktoś może mi pomoże jak mam to zrobic?