Recently I blogged a sample for using JAXB and thought of sharing it on my blog. The sample for JAXB can be found here.
Ramblings of a Developer
Recently I blogged a sample for using JAXB and thought of sharing it on my blog. The sample for JAXB can be found here.
Hi Mohamed,
I have gone through your jaxb blog and it was nice. I have one requirement and need your help on this.
Requirement: Convert XSD file to XML file using Java.
I have seach through google and found the below solution. I am getting compilation error when i compile the below program.
Program:
import java.io.File;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.stream.StreamResult;
import org.apache.xerces.xs.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import jlibs.xml.sax.XMLDocument;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import jlibs.xml.xsd.XSInstance;
import jlibs.xml.xsd.XSParser;
public class Test1{
public static void main(String[] pArgs) {
try {
String filename = “policy.xsd”;
// instance.
final Document doc = loadXsdDocument(filename);
//Find the docs root element and use it to find the targetNamespace
final Element rootElem = doc.getDocumentElement();
String targetNamespace = null;
if (rootElem != null && rootElem.getNodeName().equals(“xs:schema”))
{
targetNamespace = rootElem.getAttribute(“targetNamespace”);
}
//Parse the file into an XSModel object
XSModel xsModel = new XSParser().parse(filename);
//Define defaults for the XML generation
XSInstance instance = new XSInstance();
instance.minimumElementsGenerated = 1;
instance.maximumElementsGenerated = 1;
instance.generateDefaultAttributes = true;
instance.generateOptionalAttributes = true;
instance.maximumRecursionDepth = 0;
instance.generateAllChoices = true;
instance.showContentModel = true;
instance.generateOptionalElements = true;
//Build the sample xml doc
//Replace first param to XMLDoc with a file input stream to write to file
QName rootElement = new QName(targetNamespace, “HighSchoolTranscript”);
XMLDocument sampleXml = new XMLDocument(new StreamResult(System.out), true, 4, null);
instance.generate(xsModel, rootElement, sampleXml);
} catch (TransformerConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Document loadXsdDocument(String inputName) {
final String filename = inputName;
final DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setValidating(false);
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
Document doc = null;
try {
final DocumentBuilder builder = factory.newDocumentBuilder();
final File inputFile = new File(filename);
doc = builder.parse(inputFile);
} catch (final Exception e) {
e.printStackTrace();
// throw new ContentLoadException(msg);
}
return doc;
}
}
Compliation error :
Exception in thread “main” jlibs.core.lang.ImpossibleException: java.lang.ClassCastException: org.apache.xerces.dom.DOMXSImplementationSourceImpl cannot be cast to org.w3c.dom.DOMImplementationSource
at jlibs.xml.xsd.XSParser.(XSParser.java:52)
at jlibs.xml.xsd.XSParser.(XSParser.java:43)
at Test1.main(Test1.java:36)
Caused by: java.lang.ClassCastException: org.apache.xerces.dom.DOMXSImplementationSourceImpl cannot be cast to org.w3c.dom.DOMImplementationSource
at org.w3c.dom.bootstrap.DOMImplementationRegistry.newInstance(DOMImplementationRegistry.java:150)
at jlibs.xml.xsd.XSParser.(XSParser.java:50)
… 2 more
Line number 36 means, at XSModel xsModel = new XSParser().parse(filename); of above program.
Kindly help me. Appreciate your support. Kindly let me know if any information required. Looking forward for your support.
Hi,
I am trying the same Program and getting an same error. Could you please help on same?
use http://mvnrepository.com/artifact/xml-apis/xml-apis/1.4.01 in classpath to resolve the error