EMF, XSD to ECore mapping, Attribute vs. Element

farisola

Neues Mitglied
Hallo,
die Kurzfassung:
Bei Benutzung von XSD und EMF bekomme ich im xml output
[XML]<uhrzeit value="23:54"/>[/XML], möchte aber haben [XML]<uhrzeit>23:54</uhrzeit>[/XML]
wie muss das XSD korrekt aussehen?

Detailliert:

zum Thema: Java Generierung mit EMF aus XSD
Umgebung: Eclipse 3.7 MDT
Kurz das doing in einem Eclipse EMF Project, z.B. Namens emf_party:
1. Ich nehme ein XSD (party.xsd)
2. Ich importiere das XSD im EMF Projekt (=> party.genmodel und Party.ecore werden generiert)
3. Ich lasse aus dem Model den Java Code generieren.
4. Ich benutze den JavaCode, um dann ein xml zu schreiben.

Nun zum Problem:

Im output bekomme ich:
[XML]
<uhrzeit value="23:54"/>
[/XML]
Ich möchte aber haben:
[XML]
<uhrzeit>23:54</uhrzeit>
[/XML]

Meine Frage:
Wie muss das Schema aussehen, damit ich den gewünschten Output bekomme?


Hier die Sourcen:
xsd:
[XML]
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" ecore:nsPrefix="Party" ecore:package="Party">
<xsd:simpleType name="datumType" ecore:name="DatumType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0–3][0–9].[0–1][0–9].[0–9][0–9]"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="gastType" ecore:name="GastType">
<xsd:sequence>
<xsd:element name="getraenk" type="xsd:string"/>
<xsd:element name="zustand" type="zustandType"/>
<xsd:element name="uhrzeit">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:anyURI"/>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="partyType" ecore:name="PartyType">
<xsd:sequence>
<xsd:element name="gast" type="gastType"/>
</xsd:sequence>
<xsd:attribute name="datum" type="datumType"/>
</xsd:complexType>
<xsd:complexType name="zustandType" ecore:name="ZustandType">
<xsd:attribute name="ledig" type="xsd:boolean"/>
<xsd:attribute name="nuechtern" type="xsd:boolean"/>
</xsd:complexType>
</xsd:schema>
[/XML]

java code
Java:
  private void createPartyXml() {
    PartyFactoryImpl.init();
    PartyFactory factory = PartyFactory.eINSTANCE;
    PartyType party = factory.createPartyType();
    party.setDatum("21.12.2112");
    GastType gast = factory.createGastType();
    gast.setGetraenk("Bier");
    ZustandType zustand = factory.createZustandType();
    zustand.setLedig(true);
    zustand.setNuechtern(false);
    gast.setZustand(zustand);
    party.setGast(gast);
    UhrzeitType uhrzeit = factory.createUhrzeitType();
    uhrzeit.setValue("23:54");
    gast.setUhrzeit(uhrzeit);
    final Resource res = new XMLResourceImpl();
    res.getContents().add(party);
    FileOutputStream outputStream;
    try {
      outputStream = new FileOutputStream("party_result.xml");
      res.save(outputStream, null);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

result xml:
[XML]
<?xml version="1.0" encoding="ASCII"?>
<Party:partyType xmlns:party="platform:/resource/emf_party/model/Party.xsd" datum="21.12.2112">
<gast getraenk="Bier">
<zustand ledig="true" nuechtern="false"/>
<uhrzeit value="23:54"/>
</gast>
</Party:partyType>

[/XML]

Gruß Farisola
 
S

SlaterB

Gast
nach Lehrbuch müssten anscheinend auch getraenk und zustand eigene Elemente sein,
eine so komplizierte Definition wie für uhrzeit sollte gar nicht nötig sein müssen..

interessant wird es vielleicht, wenn du für uhrzeit auch ein Attribut definierst, hat es dann zwei Attribute?
definiere es mal wie 'SizeType' in
Definitive XML Schema Examples: Complex Types (xs:complexType)

ne Lösung oder sonstige Hinweise kann ich allerdings nicht beitragen
 

farisola

Neues Mitglied
Die Lösung des Problems:
Die generierte "resource factory" muss benutzt werden,
um die Resource zu kreieren.

Hier der Code:

Java:
  private void createPartyXmlWorking() {
    PartyFactory factory = PartyFactory.eINSTANCE;
    PartyType party = factory.createPartyType();
    party.setDatum("21.12.2112");
    GastType gast = factory.createGastType();
    gast.setGetraenk("Bier");
    ZustandType zustand = factory.createZustandType();
    zustand.setLedig(true);
    zustand.setNuechtern(false);
    gast.setZustand(zustand);
    party.setGast(gast);
    UhrzeitType uhrzeit = factory.createUhrzeitType();
    uhrzeit.setValue("23:54");
    gast.setUhrzeit(uhrzeit);
    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
      (Resource.Factory.Registry.DEFAULT_EXTENSION, 
       new PartyResourceFactoryImpl());
    
    URI uri = URI.createFileURI("party_result_right.xml");
    final Resource res = resourceSet.createResource(uri);
    res.getContents().add(party);
    try {
      res.save(null);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Ciao Farisola
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
Y Ecore to xml und von xml to Ecore XML & JSON 5
I XML to Object - Mapping mit JAXB 1.0 XML & JSON 1
M O/R-Mapping: discriminator-column und join-column mit gleichem Namen? XML & JSON 2
F XML-Schema mapping XML & JSON 6
H XML to XML Mapping XML & JSON 2
B JPA + JAXB Mapping Problem XML & JSON 2
S XML to XML Mapping XML & JSON 3
S XML-ER-Mapping XML & JSON 2
iman XML Attribute mit JAVA einlesen mit Spring Batch XML & JSON 0
L Transformer verschluckt Attribute bei der Ausgabe XML & JSON 3
W XML einlesen und Attribute auslesen XML & JSON 1
D Attribute aus XML Auslesen XML & JSON 5
P Xpath zugriff auf Attribute XML & JSON 3
R cvc-complex-type.3.2.2: Attribute 'uuid' is not allowed to appear in element 'jasperReport'. XML & JSON 4
C JAXB: XML-Elemente einlesen und als XML-Attribute ausgeben XML & JSON 7
A XML-Attribute vergleichen XML & JSON 4
Helgon MalformedByteSequenceException und null JDOM Attribute XML & JSON 5
G JAXB XML-Attribute feststellen XML & JSON 4
2 wie komme ich an die Attribute XML & JSON 3
S Attribute von Elementen auslesen mit XPath XML & JSON 2
B 2 Attribute in einer xsl XML & JSON 2
M Gegenseitig ausschließende Attribute definieren in XSD XML & JSON 7
C DOM: Attribute nicht in alphabetischer Reihenfolge schreiben XML & JSON 3
eQuest XML Jaxen Attribute auslesen XML & JSON 2
C Mit SAX Parser XML Attribute auswerten XML & JSON 3
H XML Attribute auslesen XML & JSON 5
S JAXB und viele verschachtelte Attribute XML & JSON 1
G xquery - nodes unterscheiden sich nur durch attribute :-( XML & JSON 4
L Objekt Serialisierung: Schreiben aller Attribute erzwingen XML & JSON 5
E EMF Modell um alle möglichen Attribute auszulesen XML & JSON 12
S XSD: restriction und attribute gleichzeitig XML & JSON 4
V XMLEncoder: Attribute nicht serialisieren XML & JSON 2
D Wie kann ich Namespace Attribute in den Elementen entfernen? XML & JSON 2
M Attribute parsen XML & JSON 6
D JDOM erzeugt leere xmlns Attribute XML & JSON 4
B jdom outputter ohne attribute XML & JSON 2
G Wie Attribute aus XML in ein Model schreiben (StAX)? XML & JSON 7
P Knoten-Attribute einer xsd-Datei in einem JTree auslesen XML & JSON 18
M attribute funken nicht XML & JSON 3
R Jtree + Attribute XML & JSON 6
Z DOM: Attribute in Knoten schreiben XML & JSON 2

Ähnliche Java Themen

Neue Themen


Oben