Hi Alexander,
1) On the schema element, you need to get a cursor: XmlObject.getCursor() and
use the method: XmlCursor.insertNamespace ( String prefix, String namespace )
2) You can allways look at how scomp is implemented:
org.apache.xmlbeans.impl.tool.SchemaCompiler which is a now standard way, or
use the API:
import java.util.ArrayList;
import java.util.List;
import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlBeans;
import org.apache.xmlbeans.XmlObject;
public class MySchemaCompile
{
// your schema here
private static String SCHEMA = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n" +
"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
" elementFormDefault=\"qualified\"\n" +
" attributeFormDefault=\"unqualified\" \n" +
" targetNamespace=\"http://foo/\"\n" +
" xmlns:foo=\"http://foo/\">\n" +
"\n" +
" <xsd:element name=\"bar\" type=\"xsd:string\" />\n"
"\n" +
"</xsd:schema>";
public static void main(String[] args)
{
try
{
SchemaDocument schemaDoc = SchemaDocument.Factory.parse(SCHEMA);
// check it is valid
System.out.println("===== is valid? -> "+schemaDoc.validate());
// compile
System.out.println("===== compiling?");
compile(schemaDoc);
System.out.println("END");
}
catch (Exception e)
{
System.out.println("ERROR");
e.printStackTrace();
}
}
private static void compile(SchemaDocument schemaDoc) throws Exception
{
XmlOptions options = new XmlOptions();
List<XmlError> errors = new ArrayList<XmlError>();
options.setErrorListener(errors);
options.setCompileNoAnnotations();
options.setValidateTreatLaxAsSkip();
options.setLoadLineNumbers();
// compile
XmlBeans.compileXsd(new XmlObject[] { schemaDoc },
XmlBeans.getBuiltinTypeSystem(), options);
// check validation errors
for(XmlError error : errors)
{
if (error.getSeverity() == XmlError.SEVERITY_ERROR)
{
throw new XmlException(error.getMessage(), null, errors);
}
}
}
}
I hope this helps.
Cezar
-----Original Message-----
From: Alexander Kampmann [mailto:[email protected]]
Sent: Monday, May 31, 2010 10:26 AM
To: [email protected]
Subject: problems adding namespace to a schema generated in code
hi there,
i'm using the org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument (and
related) classes to create a xml- schema in code.
It works well and I can write the schema to a file, but there are two problems:
1) I can't figure out how to add something like xmlns:tns="..." to my schema-
Element. Setting the target namespace works, but the schema is invalid without
that attribute.
2) By now I write the schema to a file and start scomp using
Runtime.getDefaultRuntime().exec(), but in my eyes it would be nicer to start
the compilation from my program directly. Is there a way to do so? I have had a
look at the scomp- sources, but I didn't get wise from it.
Thanks for your advice,
Alex
--
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]