我不太明白你的问题。
我想你的XSD会给你Java类来产生这样的XML:
<book author="Fred" title="The Lady and a Little Dog" />
您的意思是要在XML元素中设置“内部”文本,从而得到这样的XML吗?
<book>
<author>Fred</author>
<title>The Lady and a Little Dog</title>
</book>
如果是这样,请将XSD更改为此,以使用嵌套元素而不是属性:
<xs:sequence>
<xs:element name="Book">
<xs:complexType>
<xs:sequence>
<xs:element name="author" type="xs:string" />
<xs:element name="title" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
然后你就可以做到:
Book book= books.addNewBook();
book.setAuthor("Fred");
book.setTitle("The Lady and a Little Dog");
更新
好吧-我现在明白了。
试试这个:
<xs:element name="Book" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="author" type="xs:string" />
<xs:attribute name="title" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
然后:
Book book1 = books.addNewBook();
book1.setAuthor("Fred");
book1.setTitle("The Lady and a Little Dog");
book1.setStringValue("This is some text");
Book book2 = books.addNewBook();
book2.setAuthor("Jack");
book2.setTitle("The Man and a Little Cat");
book2.setStringValue("This is some more text");
它应该提供这样的XML,我认为这是您想要的:
<Book author="Fred" title="The Lady and a Little Dog">This is some text</Book>
<Book author="Jack" title="The Man and a Little Cat">This is some more text</Book>