zoukankan      html  css  js  c++  java
  • JAXB

    Usually hidden in the middle of the list of the classes derived from the types defined in an XML schema there will be one class called ObjectFactory. It's convenient to use the methods of this class because they provide an easy way of creating elements that have to be represented by a JAXBElement<?> object. Given that the top-level element of a document is represented as a JAXBElement<RulebaseType> with the tag "rulebase", one such doument object can be created by code as shown below.

    ObjectFactory objFact = new ObjectFactory();
    RulebaseType rulebase = objFact.createRulebaseType();
    JAXBElement<RulebaseType> doc = objFact.createRulebase( rulebase );

    A simple element that does not require a JAXBElement<?> wrapper is created by a straightforward method call.

    ModuleType module = objFact.createModuleType();

    JAXBElement<?> is also required for element sequences containing elements of the same type but with differing tags. Here is a schema snippet:

    <xsd:complexType name="FooBarListType">
        <xsd:sequence>
            <xsd:choice minOccurs="0" maxOccurs="unbounded">
                <xsd:element name="foo" type="FooBarType"/>
                <xsd:element name="bar" type="FooBarType"/>
            </xsd:choice>
        </xsd:sequence>
    </xsd:complexType>

    The ObjectFactory would now contain several methods for creating a FooBarListType and its offsprings. A possible sequence of calls is shown in the Java code below.

    FooBarListType fblElem = objFact.createFooBarListType();
    List<JAXBElement<FooBarType>> fbList = fblElem.getFooOrBar();
    
    // create a "foo" element
    FooBarType foo = objFact.createFooBarType();
    // ...(add attributes and components to foo)
    // Create the element <foo>...</foo>
    JAXBElement<FooBarType> fooElem = objFact.createFooBarTypeFoo( foo );
    // Add it to its parent's list.
    fbList.add( fooElem );
    
    // create a "bar" element
    FooBarType bar = objFact.createFooBarType();
    // ...(add attributes and components to bar)
    // Create the element <bar>...</bar>
    JAXBElement<FooBarType> barElem = objFact.createFooBarTypeBar( bar );
    // Add it to its parent's list.
    fbList.add( barElem );

    You may avoid these complications by subtyping FooBarType into identical types FooType and BarType.

  • 相关阅读:
    android 中ImageButton按下改变背景图片的效果
    Android根据Button状态(normal,focused,pressed)显示不同背景图片
    Android简单逐帧动画Frame的实现(三)
    Android消息总线的演进之路:用LiveDataBus替代RxBus、EventBus
    美团点评云真机平台实践
    美团客户端响应式框架EasyReact开源啦
    MCI:移动持续集成在大众点评的实践
    如何基于深度学习实现图像的智能审核?
    Android自动化页面测速在美团的实践
    美团外卖iOS多端复用的推动、支撑与思考
  • 原文地址:https://www.cnblogs.com/huey/p/5511292.html
Copyright © 2011-2022 走看看