xmlbeans - 设置复杂类型的内容

发布于 2024-07-17 19:27:27 字数 740 浏览 9 评论 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());

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何其悲哀 2024-07-24 19:27:27

我不太明白你的问题。

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

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>
野味少女 2024-07-24 19:27:27

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

也许为您的光标问题提供更多背景信息会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文