开发者

XMLBeans bounded Atom, setting content of entry

开发者 https://www.devze.com 2023-03-22 11:46 出处:网络
This is a question that is similar to this one : xmlbeans - set content of a complex type but not quite the same. What I am trying to do is to set the content of a contentEntry in an atom feed.

This is a question that is similar to this one : xmlbeans - set content of a complex type but not quite the same. What I am trying to do is to set the content of a contentEntry in an atom feed.

Here is the atom xsd definition for contentType, i.e. the content tag for entry in an atom feed.

<xs:complexType name="contentType" mixed="true">
    <xs:annotation>
        <xs:documentation>
            The Atom content construct is defined in section 4.1.3 of the format spec.
        </xs:documentation>
    </xs:annotation>
    <xs:sequence>
        <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="type" type="xs:string"/>
    <xs:attribute name="src" type="xs:anyURI"/>
    <xs:attributeGroup ref="atom:commonAttributes"/>
</xs:complexType>

After compilation by xmlbean' scomp, I get a nice jar file that makes me able to do the following.

EntryType curEntry;
curEntry = atomFeed.addNewEntry();
ContentType curContent = curEntry.addNewContent();
curContent.setBase("base");
curContent.setLang("en-EN");
curContent.setSrc("none");
curContent.setType("none");

And this is outputted as

<content xml:base="base" xml:lang="en-EN" src="none" type="none"/>

I really don't want to mess with the official (as official as 开发者_开发百科I could find) xsd for atom, but I am missing a method to be able to set the actual text representation of the curContent. Only other set functions are set(XmlObject object) and setNil().

How can I change this so that I can get :

<content xml:base="base" xml:lang="en-EN" src="none" type="none">Content of this entry</content>

Thanks


You need to drop into XmlCursor land to insert mixed content. For example,

    ContentType content = x.addNewContent();
    content.setType("none");

    XmlCursor cur = null;
    try
    {
        cur = content.newCursor();
        cur.toFirstContentToken();
        cur.insertChars("Hello World");
    }
    finally
    {
        cur.dispose();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消