xmlbeans-设置复杂类型的内容 [英] xmlbeans - set content of a complex type
问题描述
我的xsd文件包含:
<xs:sequence>
<xs:element name="Book">
<xs:complexType>
<xs:attribute name="author" type="xs:string" />
<xs:attribute name="title" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
使用xmlbeans,我可以使用以下命令轻松设置属性:
With xmlbeans, I can set the attributes easily using:
Book book= books.addNewBook();
book.setTitle("The Lady and a Little Dog");
我知道我可以使用newCursor()设置元素的内容,但这是最好的方法吗?
I know that I can use newCursor() to set the content of the element, but is this the best way?
object.newCursor().setTextValue(builer.toString());
推荐答案
我不太理解您的问题.
我认为您的XSD将为您提供Java类来生成XML,如下所示:
I think your XSD will give you Java classes to produce XML like this:
<book author="Fred" title="The Lady and a Little Dog" />
您是说要在XML元素中设置内部"文本,所以最终得到这样的XML?
Do you mean you want to set the "inner" text within an XML element, so you end up with XML like this?
<book>
<author>Fred</author>
<title>The Lady and a Little Dog</title>
</book>
如果是这样,请将您的XSD更改为此,以使用嵌套元素而不是属性:
If so, change your XSD to this, to use nested elements rather than attributes:
<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,我想这就是您想要的:
Which should give XML like this, which I think is what you want:
<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>
这篇关于xmlbeans-设置复杂类型的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!