Java Reference
In-Depth Information
3.
Create a
SourceStream
that represents the source XML contents.
4.
Create a
StreamResult
instance for your output document, which
is an HTML file in this case.
5.
Use the
Transformer
object's
transform()
method to perform
the conversion.
6.
Close all the relevant streams and file instances, as needed.
If you choose to execute the sample code, you should invoke it in the following
way, using
patients.xml
,
patients.xsl
, and
patients.html
as argu-
ments:
java org.java8recipes.chapter20.recipe20_3.TransformXml
<xmlFile><xslFile><outputFile>
20-4. Validating XML
Problem
You want to confirm that your XML is valid—that it conforms to a known document
definition or schema.
Solution
Validate that your XML conforms to a specific schema by using the
javax.xml.validation
package. The following code snippet from
org.java8recipes.chapter20.recipe20_4.ValidateXml
demonstrates
how to validate against an XML schema file:
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;