-1

How do you find all the elements which are unbounded in an XmlSchemaElement? I'm using apache's WS library...elements that look like this,

   <xs:complexType>

        <xs:sequence maxOccurs="unbounded" >

            <xs:element
                name="“Object123"
                type="“ObjectType" >

                <xs:annotation>

                    <xs:appinfo>

                        <xs:attribute
                            name="“ObjectLabel"
                            default="“Object" />
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
Hades
  • 3,916
  • 3
  • 34
  • 74

3 Answers3

0

XML schema documents are valid XML documents. I suggest you load it as you would with any other XML file and use XPath to search for elements with maxOccurs="unbounded" An example in Java:

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

// ... load your schema into xmlDocument and then do the following:

XPath xPath =  XPathFactory.newInstance().newXPath();
String expression = "//*[@maxOccurs='unbounded']";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);

// nodeList contains what you're looking for.
0

You can use a XmlPullParser class instance to achieve this.

public class UnboundItems {
  private String name;
  private String type;
  // Whatever else you need...

  public getName() { return name; }
  public getType() { return type; }
  public setName(String _name) { name = _name; }
  public setType(String _type) { type = _type; }
}    

private void parseXML(final XmlPullParser parser) throws XmlPullParserException, IOException {
  int eventType;
  ArrayList<UnboundItems> unbItems;

  while ((eventType = parser.getEventType()) != XmlPullParser.END_DOCUMENT) {
    UnboundItems currentUnboundItem;    // For current iteration
    boolean processing_unbound = false;
    String curtag = null;               //  The current tag
    switch (eventType) {
      case XmlPullParser.START_DOCUMENT:
        break;

      case XmlPullParser.START_TAG:
        curtag = parser.getName();               // Get the current tag's name
        if (curtag.equals("xs:complexType"))
          ...
        else if (curtag.equals("sequence")) {    // The unbound items will be under this tag
          currentUnboundItem = new UnboundItems();
          processing_unbound = true;
        }
        else if ((curtag.equals("element")) && (processing_unbound)) {
          // Get attribute values
          final String name = parser.getAttributeValue(null, "name");
          final String type = parser.getAttributeValue(null, "type");
          currentUnboundItem.setName(name);
          currentUnboundItem.setType(type);
          ...
        }
      }

      break;

    case XmlPullParser.END_TAG:
      curtag = parser.getName();
      if ((curtag.equals("xs:complexType")) && (processing_unbound)) {
        // Probably add the UnboundItem to your array here
        unbItems.add(currentUnboundItem);
        ...
        processing_unbound = false;
      }

      break;
    }

    eventType = parser.next();                  // Next event
  }
}

As you may see, you can make it as complex as you need. This XML document is capable to be processed this way, so it's up to you how complex it is and your needs. Some useful links:

Community
  • 1
  • 1
nKn
  • 13,691
  • 9
  • 45
  • 62
  • 1
    not really the answer cause I'm using apache's ws xml parser...but I'll give you the bounty for the effort involved... – Hades Mar 08 '14 at 23:33
0

So this is what I was looking for and I finally found it,

http://www.docjar.com/html/api/org/apache/ws/commons/schema/SchemaBuilder.java.html

So according to that, it just takes the max capacity of a long and then uses that as the maximum unbounded capacity of a element.

XmlSchemaSequence sequence = (XmlSchemaSequence) childParticle;
if(sequence.getMaxOccurs() == Long.MAX_VALUE){
   //unbounded element
}
Hades
  • 3,916
  • 3
  • 34
  • 74