使用 ISO-8859-1 验证 XSD 架构
Validate XSD Schema with ISO-8859-1
我有一个XML。而这个 XML 我必须用 e XSD 来验证。 XSD 请求 ISO-8859-1 编码。
我已经尝试使用此代码,但它不起作用。看谁错了?
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;
}
}
顶部来自 XSD
<?xml version='1.0' encoding='ISO-8859-1' ?>
按以下方式做:
public class XMLUtils {
private XMLUtils() {}
// validate SAX and external XSD
public static boolean validateWithExtXSDUsingSAX(String xml, String xsd)
throws ParserConfigurationException, IOException
{
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
SAXParser parser = null;
try {
factory.setSchema(schemaFactory.newSchema(new Source[] {new StreamSource( xsd )}));
parser = factory.newSAXParser();
}
catch (SAXException se) {
System.out.println("SCHEMA : " + se.getMessage()); // problem in the XSD itself
return false;
}
XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(
new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {
System.out.println("WARNING: " + e.getMessage()); // do nothing
}
public void error(SAXParseException e) throws SAXException {
System.out.println("ERROR : " + e.getMessage());
throw e;
}
public void fatalError(SAXParseException e) throws SAXException {
System.out.println("FATAL : " + e.getMessage());
throw e;
}
}
);
reader.parse(new InputSource(xml));
return true;
}
catch (ParserConfigurationException pce) {
throw pce;
}
catch (IOException io) {
throw io;
}
catch (SAXException se){
return false;
}
}
public static void main (String args[]) throws Exception{
System.out.println
(XMLUtils.validateWithExtXSDUsingSAX
("c:/temp/YourXML.xml", "c:/temp/YourXSD.xsd"));
}
}
output :(if it is validated) true
希望对你有所帮助..
我有方法验证XML。我将输出器设置为 "setEncoding("ISO-8859-1")"
为什么最后 XML 文件以
开头
<?xml version="1.0" encoding="UTF-8"?>
方法验证XML
private void validateXML(Document doc) throws Exception {
XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat().setEncoding("ISO-8859-1"));
String docStr = xmlOutput.outputString(doc);
//InputStream inputStream = new ByteArrayInputStream(docStr.getBytes(Charset.forName("ISO-8859-1")));
byte[] bytes = docStr.getBytes(Charset.forName("ISO-8859-1"));
InputStream inputStream = new ByteArrayInputStream(bytes);
Source xml = new StreamSource(inputStream);
File xsdfile = new File(this.getClass().getClassLoader().getResource(SCHEMA_PATH).getFile());
SchemaFactory schemaFactor = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactor.newSchema(xsdfile);
Validator validator = schema.newValidator();
validator.validate(xml);
}
我有一个XML。而这个 XML 我必须用 e XSD 来验证。 XSD 请求 ISO-8859-1 编码。
我已经尝试使用此代码,但它不起作用。看谁错了?
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;
}
}
顶部来自 XSD
<?xml version='1.0' encoding='ISO-8859-1' ?>
按以下方式做:
public class XMLUtils {
private XMLUtils() {}
// validate SAX and external XSD
public static boolean validateWithExtXSDUsingSAX(String xml, String xsd)
throws ParserConfigurationException, IOException
{
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
SAXParser parser = null;
try {
factory.setSchema(schemaFactory.newSchema(new Source[] {new StreamSource( xsd )}));
parser = factory.newSAXParser();
}
catch (SAXException se) {
System.out.println("SCHEMA : " + se.getMessage()); // problem in the XSD itself
return false;
}
XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(
new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {
System.out.println("WARNING: " + e.getMessage()); // do nothing
}
public void error(SAXParseException e) throws SAXException {
System.out.println("ERROR : " + e.getMessage());
throw e;
}
public void fatalError(SAXParseException e) throws SAXException {
System.out.println("FATAL : " + e.getMessage());
throw e;
}
}
);
reader.parse(new InputSource(xml));
return true;
}
catch (ParserConfigurationException pce) {
throw pce;
}
catch (IOException io) {
throw io;
}
catch (SAXException se){
return false;
}
}
public static void main (String args[]) throws Exception{
System.out.println
(XMLUtils.validateWithExtXSDUsingSAX
("c:/temp/YourXML.xml", "c:/temp/YourXSD.xsd"));
}
}
output :(if it is validated) true
希望对你有所帮助..
我有方法验证XML。我将输出器设置为 "setEncoding("ISO-8859-1")"
为什么最后 XML 文件以
开头<?xml version="1.0" encoding="UTF-8"?>
方法验证XML
private void validateXML(Document doc) throws Exception {
XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat().setEncoding("ISO-8859-1"));
String docStr = xmlOutput.outputString(doc);
//InputStream inputStream = new ByteArrayInputStream(docStr.getBytes(Charset.forName("ISO-8859-1")));
byte[] bytes = docStr.getBytes(Charset.forName("ISO-8859-1"));
InputStream inputStream = new ByteArrayInputStream(bytes);
Source xml = new StreamSource(inputStream);
File xsdfile = new File(this.getClass().getClassLoader().getResource(SCHEMA_PATH).getFile());
SchemaFactory schemaFactor = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactor.newSchema(xsdfile);
Validator validator = schema.newValidator();
validator.validate(xml);
}