gpt4 book ai didi

java - xmlbeans - 设置复杂类型的内容

转载 作者:行者123 更新时间:2023-12-01 16:12:09 26 4
gpt4 key购买 nike

我的 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,我可以使用以下方式轻松设置属性:

    Book book= books.addNewBook();
book.setTitle("The Lady and a Little Dog");

我知道我可以使用 newCursor() 来设置元素的内容,但这是最好的方法吗?

object.newCursor().setTextValue(builer.toString());

最佳答案

我不太明白你的问题。

我认为您的 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>

关于java - xmlbeans - 设置复杂类型的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/736913/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com