在XmlSchemaElement中查找所有无界元素

时间:2014-03-05 04:53:26

标签: java android xml

如何找到XmlSchemaElement中无限制的所有元素?我正在使用apache的WS库......看起来像这样的元素,

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

3 个答案:

答案 0 :(得分:0)

XML架构文档是有效的XML文档。我建议您像使用任何其他XML文件一样加载它,并使用XPath搜索maxOccurs="unbounded"的元素 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.

答案 1 :(得分:0)

您可以使用XmlPullParser类实例来实现此目的。

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

正如您所看到的,您可以根据需要使其变得复杂。这个XML文档能够以这种方式处理,因此它取决于它的复杂程度和需求。一些有用的链接:

答案 2 :(得分:0)

所以这就是我想要的,我终于找到了它,

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

因此,根据它,它只需要一个long的最大容量,然后将其用作元素的最大无界容量。

XmlSchemaSequence sequence = (XmlSchemaSequence) childParticle;
if(sequence.getMaxOccurs() == Long.MAX_VALUE){
   //unbounded element
}