Created
February 5, 2014 02:38
-
-
Save playerjamesbattleground/8816576 to your computer and use it in GitHub Desktop.
Generate xml from xsd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sixtree.xs2xml; | |
import java.io.File; | |
import javax.xml.namespace.QName; | |
import javax.xml.transform.TransformerConfigurationException; | |
import javax.xml.transform.stream.StreamResult; | |
import jlibs.xml.Namespaces; | |
import jlibs.xml.sax.XMLDocument; | |
import jlibs.xml.xsd.XSInstance; | |
import jlibs.xml.xsd.XSParser; | |
import org.apache.xerces.xs.XSModel; | |
import org.xml.sax.SAXException; | |
/** | |
* | |
* @author leijiang | |
* @param arg0 | |
* : source xsd location | |
* @param arg1 | |
* : destination xml location | |
* @param arg2 | |
* : root name | |
* @param arg3 | |
* : target namespace (if empty string is passed in, convert to null) | |
*/ | |
public class XMLGenerator { | |
public static void main(String[] args) { | |
File xsd = new File(args[0]); | |
XSInstance xsi = new XSInstance(); | |
xsi.generateOptionalAttributes = true; | |
xsi.generateOptionalElements = true; | |
args[3] = args[3].equalsIgnoreCase("") ? null : args[3]; | |
QName root = new QName(args[3], args[2]); | |
XSModel xsmodel = new XSParser().parse(xsd.getAbsolutePath()); | |
xsmodel.getNamespaceItems(); | |
XMLDocument sampleXml; | |
try { | |
sampleXml = new XMLDocument(new StreamResult(args[1]), false, 4, | |
null); | |
xsi.generate(xsmodel, root, sampleXml); | |
} catch (TransformerConfigurationException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Sample xml has been generated"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Me too facing the issue and any help would be highly appreciated. I may be missing a jar file in the classpath but couldn't figure out.
Please help!
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.xerces.xs.XSObjectList.isEmpty()Z
at jlibs.xml.xsd.XSInstance$XSSampleVisitor$1.preProcess(XSInstance.java:265)
at jlibs.xml.xsd.XSInstance$XSSampleVisitor$1.preProcess(XSInstance.java:238)
at jlibs.core.graph.WalkerUtil.walk(WalkerUtil.java:41)
at jlibs.xml.xsd.XSInstance.generate(XSInstance.java:126)
at jlibs.xml.xsd.XSInstance.generate(XSInstance.java:93)
at com.mkls.ppa.XMLGenerator1.main(XMLGenerator1.java:64)