XSD Datei erstellen

davidh38

Bekanntes Mitglied
public static void main (String [] adil){

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd", new XSDResourceFactoryImpl());

XSDPrototypicalSchema schema = new XSDPrototypicalSchema();
schema.initializePrototypeSchema();
schema.saveSchema("C:\b.xsd");




}
unknown protocol: c

Hallo Leute, mein Code ist der Obige. Ich habe vor eine XSD-Datei zu erstellen, also erstmal einen prototypen und dann auszudrucken. Wo ist mein Fehler? Ich finde leider auch kein Tutorial, wie ich solche Klassen benutzen kann. Weiß jemand von euch Rat?
 
Zuletzt bearbeitet:
Ich glaube du musst die File location im URI format uebergeben. scheme://filelocation.
Da du kein scheme(protocol) angegeben hast glaubt er 'C' waere das Protocol, dass er natuerlich nicht kennt.

Entweder du definierst die URI korrekt:
Java:
schema.saveSchema("file:/C:/b.xsd");

oder du laest dir laesst dir von File die richtige URI erzeugen:
Java:
schema.saveSchema(new File("C:\\b.xsd").toURI())
 
[c]Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(...)[/c]

Ich hoffe der Code ist ein Spaß?!?

Das ist Standalone EMF, wenn auch etwas umständlich geschrieben.

Mach mal so:

Java:
public class Snippet {

	public static void main(String[] args) {
		createXSDSchema();
	}
	
	public static XSDSchema createXSDSchema() {
		try {
			// Create a resource set to manage the different resources
			ResourceSet set = new ResourceSetImpl();
			set.getResourceFactoryRegistry().getExtensionToFactoryMap()
					.put("xsd", new XSDResourceFactoryImpl());

			// Create a resource for this file.
			Resource resource = set.createResource(URI
					.createFileURI("foobar.xsd"));
			// Create the root XSDSchema object
			XSDSchema xsdSchema = XSDFactory.eINSTANCE.createXSDSchema();
			// set the schema for schema QName prefix to "xsd"
			xsdSchema.setSchemaForSchemaQNamePrefix("xsd");
			// put the following namespace in the root schema namespace map
			// xsd:[url=http://www.w3.org/2001/XMLSchema]XML Schema[/url]
			xsdSchema.getQNamePrefixToNamespaceMap().put(
					xsdSchema.getSchemaForSchemaQNamePrefix(),
					XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
			// We call updateElement to synchronize the MOF model with the
			// underlying DOM model
			// This should only have to be done after creating a new model

			// Add the root schema to the resource that was created above
			resource.getContents().add(xsdSchema);
			
			
			//now add some content
			XSDFactory factory = XSDFactory.eINSTANCE;
			XSDElementDeclaration elementDeclaration = factory.createXSDElementDeclaration();
			elementDeclaration.setName("Foobar");
			elementDeclaration.setTypeDefinition(xsdSchema.getSchemaForSchema().getSimpleTypeIdMap().get("string"));
			
			xsdSchema.getContents().add(elementDeclaration);
			
			// Save the contents of the resource to the file system.
			resource.save(Collections.EMPTY_MAP);
			return xsdSchema;
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return null;
	}
}


Das gibt dann

[XML]<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Foobar" type="xsd:string"/>
</xsd:schema>
[/XML]

Du brauchst am Anfang etwas Boilerplate Code um das Schema und das ResourceSet zu initialisieren, danach ist es dann recht einfach mit XML Schema Infoset zu arbeiten.
 
Tutorial über erstellen eines XSD kannst du hier lesen:
JAXB zur XML-Verarbeitung

Was ich auch ganz interessant finde ist, wenn du maven verwendest ist das Plugin schemagen:

[XML]
<groupId>com.sun.tools.jxc.maven2</groupId>
<artifactId>maven-jaxb-schemagen-plugin</artifactId>
<version>1.2</version>
[/XML]
 

Zurück
Oben