0

我的 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());
4

2 回答 2

1

我不太明白你的问题。

我认为您的 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>
于 2009-05-20T07:14:30.440 回答
0

我不确定这是否正是您要问的,但使用 XMLBeans 设置属性或元素值的最佳方法是使用 XMLBeans 生成的 getter 和 setter。

也许为您的光标问题提供更多上下文会有所帮助。

于 2009-05-20T05:48:32.227 回答