Pergunta

I need the contentModel for complex type declared in xsd my XSD is as follows:

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

i am using the following code for that :

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

        }

        }

but it given NullPointerException for the SchemaParticle object which is returned by getContentModel()

the code works fine for the following xsd:

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

Foi útil?

Solução

Found a solution :

//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);
            }
        }

The Difference in handling the single child and multiple children is because of the difference in the type of the Particle , as it is ELEMENT for single child and SEQUENCE for the multiple childrens. Ref for details:http://xmlbeans.apache.org/docs/2.0.0/reference/org/apache/xmlbeans/SchemaParticle.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top