我在List对象中有XML,需要根据XSD进行验证。到目前为止,我只能验证XML文件。到目前为止,我正在将列表写入一个临时文件并验证该临时文件。我真的很想消除对临时文件的需求。我的问题是javax.xml.validation.Validator.validate需要一个Source,但我不知道如何将List放入Source。
下面是我使用临时文件的工作源代码。
static String validate(List<String> xmlData, Schema schema) throws Exception {
File tmpFile = File.createTempFile("temp", ".xml"); // TODO: delete
StringBuilder exceptionList = new StringBuilder();
try {
Validator validator = schema.newValidator();
final List<SAXParseException> exceptions = new LinkedList<SAXParseException>();
validator.setErrorHandler(new ErrorHandler()
{
@Override
public void warning(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
@Override
public void fatalError(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
@Override
public void error(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
});
// TODO: remove this block
FileWriter fr = new FileWriter(tmpFile);
for (String str: xmlData) {
fr.write(str + System.lineSeparator());
}
fr.close();
//
validator.validate(new StreamSource(tmpFile)); // TODO: Here need xmlData instead
if (! exceptions.isEmpty() ) {
exceptions.forEach((temp) -> {
exceptionList.append(String.format("lineNumber: %s; columnNumber: %s; %s%s",
temp.getLineNumber(),temp.getColumnNumber(),temp.getMessage(),System.lineSeparator()));
});
}
return exceptionList.toString();
} catch (SAXException | IOException e) {
e.printStackTrace();
throw e;
} finally {
if (tmpFile.exists()) { tmpFile.delete(); } // TODO: delete
}
}
编辑:为了子孙后代,这里有新的代码:
static String validate(String xmlData, Schema schema) throws Exception {
Reader sourceReader = null;
StringBuilder exceptionList = new StringBuilder();
try {
Validator validator = schema.newValidator();
final List<SAXParseException> exceptions = new LinkedList<SAXParseException>();
validator.setErrorHandler(new ErrorHandler()
{
@Override
public void warning(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
@Override
public void fatalError(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
@Override
public void error(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
});
sourceReader = new StringReader(xmlData);
validator.validate(new StreamSource(sourceReader));
if (! exceptions.isEmpty() ) {
exceptions.forEach((temp) -> {
exceptionList.append(String.format("lineNumber: %s; columnNumber: %s; %s%s",
temp.getLineNumber(),temp.getColumnNumber(),temp.getMessage(),System.lineSeparator()));
});
}
return exceptionList.toString();
} catch (SAXException | IOException e) {
e.printStackTrace();
throw e;
} finally {
if (sourceReader != null) { sourceReader.close(); }
}
}
您可以组装一个String,然后将其包装在阅读器中,如下所示:https://www.baeldung.com/java-convert-string-to-reader
然后您可以使用它来创建StreamSource:https://docs.oracle.com/javase/7/docs/api/javax/xml/transform/stream/StreamSource.html#StreamSource(java.io.Reader(
javadoc是您的朋友StreamSource
类有一个接受InputStream
参数的构造函数
ByteArrayInputStream扩展了InputStream
,因此只需从XML字符串创建一个ByteArrayInputStream
即可。
// import java.util.stream.Collectors;
String str = xmlData.stream()
.collect(Collectors.joining());
byte[] bytes = str.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
StreamSource source = new StreamSource(bais);
或者,您可以使用接受Reader
参数的StreamSource
构造函数,并从XML字符串创建StringReader。
String str = xmlData.stream()
.collect(Collectors.joining());
StringReader sr = new StringReader(str);
StreamSource source = new StreamSource(sr);