私はXMLを持っています。そして、このXMLはe XSDで検証する必要があります。 XSDはISO-8859-1エンコーディングを要求しました。ISO-8859-1でXSDスキーマを検証する
私はこのコードを試しましたが、動作しません。誰かに不具合を見なさいか。
public boolean validateXML(Document doc) throws ParserConfigurationException, IOException {
XMLOutputter xmlOutput = new XMLOutputter();
String xml = xmlOutput.outputString(doc);
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setValidating(true);
final Schema schema = sf.newSchema(new StreamSource(getClass().getResourceAsStream(SCHEMA_PATH)));
factory.setSchema(schema);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(xml);
is.setEncoding("ISO-8859-1");
builder.parse(is);
return true;
} catch (ParserConfigurationException pce) {
throw pce;
} catch (IOException io) {
throw io;
} catch (SAXException se) {
return false;
}
}
そして
<?xml version='1.0' encoding='ISO-8859-1' ?>
エンコーディングで見つめXMLファイルである理由
購入に出力手段を設定しますXSDのXML宣言では、XSDを使用するXMLファイルのエンコーディングとは関係ありません。指定されている限り、エンコーディングを使用することも、デフォルト(UTF-8)を使用することもできます。 – mjn