Javaでxml、xsdを入力して検証しようとしていました。次に、それらが真であることがわかった場合は、入力 xml を他の xml に変換する必要があります。xml と xsd が true を検証していないような方法でプログラムを作成しました... xsl を使用して xml を他の xml に変換できますが、これは望んでいません。助けが必要です。
コードは次のとおりです。このコードでは、XsdFile、XslFile はすべて、テストしたいフォルダーに事前定義されています。それらは単なるオブジェクトです。代わりに、ファイル「c:\abc.xml」のように考えます。if(doc!=null)という条件だけに関心があります。うまく機能するためには、何を与える必要がありますか。検証は失敗しますが、次のコードは機能しています。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
factory
.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaSource",
XsdFile);
try {
System.out.println();
DocumentBuilder parser = factory.newDocumentBuilder();
Document doc = parser.parse(XmlFile);
if (doc != null) {
Source xmlInput = new StreamSource(new File(XmlFile));
Source xsl = new StreamSource(new File(inputXslFile));
Result xmlOutput = new StreamResult(new File(transformedXml));
try {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(xsl);
transformer.transform(xmlInput, xmlOutput);
System.out.println("The transformed xml is:" + xmlOutput);
} catch (TransformerException e) {
// Handle.
}
}
} catch (ParserConfigurationException e) {
System.out.println("Parser not configured: " + e.getMessage());
} catch (SAXException e) {
System.out.print("Parsing XML failed due to a "
+ e.getClass().getName() + ":");
System.out.println(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
}