如何获取复杂数据类型的内容模型

时间:2014-01-16 13:24:45

标签: xml xsd schema xmlbeans

我需要在xsd中声明的复杂类型的contentModel我的XSD如下:

<element name="add">
<complexType>
 <sequence>
  <element name="no1" type="xsd:int" minOccurs="0"/>
  <element name="no2" type="xsd:int" minOccurs="1" maxOccurs="5"/>
 </sequence>
</complexType>

我正在使用以下代码:

//elem is ScheamType for the rootnode , in this case it is add
    if (elem != null) {

        System.out.println("\ncontent of "+elem +"   is    "+elem.getContentType());
         if(elem.getContentType()==3||elem.getContentType()==4)

        {   
             SchemaParticle particle=elem.getContentModel();

        for( SchemaParticle p:particle.getParticleChildren())

        {

             System.out.println("\nchild:"+p.getName()+"\ttype:"+p.getType().getName().getLocalPart());

        }

        }

但它给出了由getContentModel()返回的SchemaParticle对象的NullPointerException

代码适用于以下xsd:

<complexType name="Employee"> <sequence> <element name="empid" type="xsd:int"/> <element name="empname" nillable="true" type="xsd:string"/> </sequence> </complexType>

1 个答案:

答案 0 :(得分:0)

找到解决方案:

//The elem is the SchemaType variable
SchemaParticle rootSchemaParticle = elem.getContentModel();
        SchemaLocalElement rootSchemaLocalElement = (SchemaLocalElement) rootSchemaParticle;
        //Get the SchemaType for the element.
        SchemaType rootSchemaType = rootSchemaLocalElement.getType();
        //Get the content Model of the root Element ie; the Children Structure for elem
        SchemaParticle childrenSchemaparticle = rootSchemaType.getContentModel();

        if (childrenSchemaparticle != null) {// Operation has non zero child element
            if (childrenSchemaparticle.countOfParticleChild() > 0) {// multiple child elements
                for (int i = 0; i < childrenSchemaparticle
                        .countOfParticleChild(); i++) {
                    paraName = childrenSchemaparticle.getParticleChild(i).getName().getLocalPart();
                    SchemaLocalElement childSchemaLocalElement = (SchemaLocalElement) childrenSchemaparticle.getParticleChild(i);
                    minOccur = childSchemaLocalElement.getMinOccurs().longValue();
                    maxOccur = childSchemaLocalElement.getMaxOccurs().longValue();
                    SchemaType childSchemaType = childSchemaLocalElement.getType();
                    dataType = childSchemaType.getName().getLocalPart();
                    System.out.println("parameter Name "+paraName + " Datatype "+dataType+" Minoccur "+minOccur+" MaxOccur "+maxOccur);                 
                }
            } else {// Single child                 
                paraName = childrenSchemaparticle.getName().getLocalPart();
                minOccur = childrenSchemaparticle.getMinOccurs().longValue();
                maxOccur = childrenSchemaparticle.getMaxOccurs().longValue();
                SchemaLocalElement childSchemaLocalElement = (SchemaLocalElement) childrenSchemaparticle;
                SchemaType childSchemaType = childSchemaLocalElement.getType();
                dataType = childSchemaType.getName().getLocalPart();
                System.out.println("parameter Name "+paraName + " Datatype "+dataType+" Minoccur "+minOccur+" MaxOccur "+maxOccur);
            }
        }

处理单个孩子和多个孩子的区别在于粒子类型的不同,因为它是单个孩子的ELEMENT和多个孩子的SEQUENCE。详情请参阅:http://xmlbeans.apache.org/docs/2.0.0/reference/org/apache/xmlbeans/SchemaParticle.html