2009-04-10 77 views
0

我的XSD文件包括:XMLBeans的 - 复杂类型的集内容

   <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()); 

回答

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"); 

UPDATE

好的 - 我现在明白了。

试试这个:

<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> 
+0

这是我会像我的xml看起来像: 这是一些文字 我怎样才能把”这是一些文字“位? 谢谢 – dogbane 2009-05-20 12:10:04

0

我不知道这是否是你问什么,而是要设置属性或使用XMLBeans的要素价值的最佳方法是使用XMLBeans的生成getter和setter。

也许你的光标问题稍微多一点上下文会有帮助。