import
com.sun.xml.internal.ws.developer.ValidationErrorHandler;
import
org.w3c.dom.*;
import
org.w3c.dom.bootstrap.DOMImplementationRegistry;
import
org.w3c.dom.ls.*;
import
javax.xml.XMLConstants;
import
java.io.OutputStreamWriter;
public
class
SimpleDOML3MemXSD {
public
static
void
main(String args[]) {
Document doc;
try
{
DOMImplementationLS ls = (DOMImplementationLS) DOMImplementationRegistry.
newInstance().getDOMImplementation(
"LS"
);
LSParser builder = ls.createLSParser(
DOMImplementationLS.MODE_SYNCHRONOUS,
doc = builder.parseURI(args[
0
]);
DOMConfiguration config = doc.getDomConfig();
config.setParameter(
"schema-type"
, XMLConstants.W3C_XML_SCHEMA_NS_URI);
config.setParameter(
"validate"
,
true
);
config.setParameter(
"error-handler"
,
new
StdErrorHandler());
Element root = doc.getDocumentElement();
Text text = doc.createTextNode(
"Training text"
);
root.appendChild(text);
doc.normalizeDocument();
LSOutput target = ls.createLSOutput();
target.setCharacterStream(
new
OutputStreamWriter(System.out));
ls.createLSSerializer().write(doc, target);
}
catch
(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
System.exit(
0
);
}
}
private
static
class
StdErrorHandler
implements
DOMErrorHandler {
public
boolean
handleError(DOMError e) {
String prefix =
"Severity "
;
if
(e.getLocation().getLineNumber() != -
1
) {
prefix =
"Line "
+ e.getLocation().getLineNumber()
+
" column "
+ e.getLocation().getColumnNumber()
+
", severity "
;
}
System.err.println(
prefix + e.getSeverity()
+
" issue: "
+ e.getMessage());
return
true
;
}
}
}