xmlbeans - set content of a complex type

我们两清 提交于 2019-12-25 04:32:36

问题


My xsd file contains:

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

With xmlbeans, I can set the attributes easily using:

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

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

回答1:


I don't quite understand your question.

I think your XSD will give you Java classes to produce XML like this:

<book author="Fred" title="The Lady and a Little Dog" />

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>

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>

Then you'll simply be able to do:

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

UPDATE

OK - I understand now.

Try this:

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

And then:

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

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>



回答2:


I'm not sure if this is exactly what you're asking, but the best way to set the value of attributes or elements using XMLBeans is to use the XMLBeans-generated getters and setters.

Maybe a little more context for your cursor question would be helpful.



来源:https://stackoverflow.com/questions/736913/xmlbeans-set-content-of-a-complex-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!