Forked from playerjamesbattleground/XmlGenerator.java
Created
December 16, 2015 23:44
-
-
Save stevengonsalvez/325e095e29e8e5f55bda to your computer and use it in GitHub Desktop.
Generate xml from xsd
This file contains 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