Could you please help me with the following problem:
I have an xml schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://uni.tuebingen.de/sfs/testexample" elementFormDefault="qualified"
targetNamespace="http://uni.tuebingen.de/sfs/testexample">
<xs:element name="example">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="text"/>
<!--<xs:element name="text" type="xs:string"/>-->
<xs:element ref="tokens"/>
</xs:choice>
<xs:attribute name="lang" type="xs:string"/>
<xs:attribute name="source" type="xs:anyURI"/>
</xs:complexType>
</xs:element>
<xs:element name="text" type="xs:string"/>
<xs:element name="tokens">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="token"/>
</xs:sequence>
<xs:attribute name="charOffsets" type="xs:boolean"/>
</xs:complexType>
</xs:element>
<xs:element name="token">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="ID" type="xs:ID"/>
<xs:attribute name="start" type="xs:int"/>
<xs:attribute name="end" type="xs:int"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
and a simple example data file that validates against this scheme:
<?xml version="1.0" encoding="UTF-8"?>
<example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://uni.tuebingen.de/sfs/testexample">
<text>Ich bin im Sande eingeschlafen.</text>
</example>
I've generated code and binding from the Schema with CodeGen and I've run
binding compiler on them. The generated binding looks like this:
<binding xmlns:tns="http://uni.tuebingen.de/sfs/testexample" name="binding"
package="de.tuebingen.uni.sfs.testexample">
<namespace uri="http://uni.tuebingen.de/sfs/testexample" default="elements"/>
<mapping class="de.tuebingen.uni.sfs.testexample.Example" name="example">
<collection get-method="getChoices" set-method="setChoices"
create-type="java.util.ArrayList">
<structure type="de.tuebingen.uni.sfs.testexample.Example$Choice"
ordered="false" choice="true">
<structure type="de.tuebingen.uni.sfs.testexample.Text"
test-method="ifText" get-method="getText" set-method="setText"
usage="optional"/>
<structure type="de.tuebingen.uni.sfs.testexample.Tokens"
test-method="ifTokens" get-method="getTokens" set-method="setTokens"
usage="optional"/>
</structure>
</collection>
<value style="attribute" name="lang" get-method="getLang"
set-method="setLang" usage="optional"/>
<value style="attribute" name="source" get-method="getSource"
set-method="setSource" usage="optional"/>
</mapping>
<mapping class="de.tuebingen.uni.sfs.testexample.Text" name="text">
<value style="element" name="text" get-method="getText"
set-method="setText"/>
</mapping>
<mapping class="de.tuebingen.uni.sfs.testexample.Tokens" name="tokens">
<collection item-type="de.tuebingen.uni.sfs.testexample.Token"
get-method="getTokens" set-method="setTokens"
create-type="java.util.ArrayList"/>
<value style="attribute" name="charOffsets" get-method="getCharOffsets"
set-method="setCharOffsets" usage="optional"/>
</mapping>
<mapping class="de.tuebingen.uni.sfs.testexample.Token" name="token">
<value style="text" get-method="getString" set-method="setString"/>
<value style="attribute" name="ID" get-method="getID" set-method="setID"
usage="optional"/>
<value style="attribute" name="start" get-method="getStart"
set-method="setStart" usage="optional"/>
<value style="attribute" name="end" get-method="getEnd" set-method="setEnd"
usage="optional"/>
</mapping>
</binding>
Here is my test to marshal/unmarshal the example data given above:
public class MarshTest02 {
public static void main(String[] args) {
String inputFileName = "data/test_example.xml";
String outputFileName = "data/test_example_chng.xml";
try {
// unmarshal:
IBindingFactory bfact = BindingDirectory.getFactory(Example.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
FileInputStream in = new FileInputStream(inputFileName);
Example myData = (Example)uctx.unmarshalDocument(in, null);
Example.Choice choiceText
= new Example.Choice();
Text textNode = new Text();
textNode.setText("I went to sleep on the sand.");
choiceText.setText(textNode);
myData.getChoices().add(choiceText);
// marshal object back out to file
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(2);
FileOutputStream out = new FileOutputStream(outputFileName);
mctx.setOutput(out, null);
mctx.marshalDocument(myData);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (JiBXException e) {
e.printStackTrace();
System.exit(1);
}
}
}
When I run this code, I get an error: org.jibx.runtime.JiBXException: Expected
"{http://uni.tuebingen.de/sfs/testexample}text" start tag, found
"{http://uni.tuebingen.de/sfs/testexample}text" end tag I.e. it cannot
unmarshal <text>Ich bin im Sande eingeschlafen.</text> because it expects
another <text> element inside first <text> element for some reason. For example
created inside MarshTest02 Text object will be murshalled like this:
<example xmlns="http://uni.tuebingen.de/sfs/testexample">
<text>
<text>I went to sleep on the sand.</text>
</text>
</example>
Why is that?
If I change in my schema the way I define text element to this:
<xs:element name="example">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="text" type="xs:string"/>
<xs:element ref="tokens"/>
</xs:choice>
<xs:attribute name="lang" type="xs:string"/>
<xs:attribute name="source" type="xs:anyURI"/>
</xs:complexType>
</xs:element>
everything works fine and element <text> is marshalled/unmarshalled correctly.
But these two definitions are equivalent, aren't they? I need to use the
definition of the first schema, please help me to find out how to make it work.
Or do I do smth wrong?
------------------------------------------------------------------------------
Are you an open source citizen? Join us for the Open Source Bridge conference!
Portland, OR, June 17-19. Two days of sessions, one day of unconference: $250.
Need another reason to go? 24-hour hacker lounge. Register today!
http://ad.doubleclick.net/clk;215844324;13503038;v?http://opensourcebridge.org
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users