2012-02-15 77 views
0

我想使用java.I的xsom找出xsd元素的最小出现最大值出现得到了这个代码来找出复杂的元素。任何人都可以帮助我在所有的发现occurence的XSD element.Atlest给我一个代码片段与类和方法被用来找到发生如何使用xsom在xsd中查找min-max元素

xmlfile = "Calendar.xsd" 
XSOMParser parser = new XSOMParser(); 

parser.parse(new File(xmlfile)); 
XSSchemaSet sset = parser.getResult(); 
XSSchema s = sset.getSchema(1); 
if (s.getTargetNamespace().equals("")) // this is the ns with all the stuff 
     // in 
{ 
    // try ElementDecls 
    Iterator jtr = s.iterateElementDecls(); 
    while (jtr.hasNext()) 
    { 
    XSElementDecl e = (XSElementDecl) jtr.next(); 
    System.out.print("got ElementDecls " + e.getName()); 
    // ok we've got a CALENDAR.. what next? 
    // not this anyway 
    /* 
    * 
    * XSParticle[] particles = e.asElementDecl() for (final XSParticle p : 
    * particles) { final XSTerm pterm = p.getTerm(); if 
    * (pterm.isElementDecl()) { final XSElementDecl ed = 
    * pterm.asElementDecl(); System.out.println(ed.getName()); } 
    */ 
    } 

    // try all Complex Types in schema 
    Iterator<XSComplexType> ctiter = s.iterateComplexTypes(); 
    while (ctiter.hasNext()) 
    { 
    // this will be a eSTATUS. Lets type and get the extension to 
    // see its a ENUM 
    XSComplexType ct = (XSComplexType) ctiter.next(); 
    String typeName = ct.getName(); 
    System.out.println(typeName + newline); 

    // as Content 
    XSContentType content = ct.getContentType(); 
    // now what? 
    // as Partacle? 
    XSParticle p2 = content.asParticle(); 
    if (null != p2) 
    { 
     System.out.print("We got partical thing !" + newline); 
     // might would be good if we got here but we never do :-(
    } 

    // try complex type Element Decs 
    List<XSElementDecl> el = ct.getElementDecls(); 
    for (XSElementDecl ed : el) 
    { 
     System.out.print("We got ElementDecl !" + ed.getName() + newline); 
     // would be good if we got here but we never do :-(
    } 

    Collection<? extends XSAttributeUse> c = ct.getAttributeUses(); 
    Iterator<? extends XSAttributeUse> i = c.iterator(); 
    while (i.hasNext()) 
    { 
     XSAttributeDecl attributeDecl = i.next().getDecl(); 
     System.out.println("type: " + attributeDecl.getType()); 
     System.out.println("name:" + attributeDecl.getName()); 
    } 
    } 
} 

回答

1

假设你是指com.sun.xml.xsom,发生特定于粒子(元素不是唯一的粒子)。

以下是这些API:maxOccursminOccurs

为一个信号源,看看如何使用XSOM请看看here遍历模式树。它基本上显示了访问者模式如何与XSOM(Sun为其构建一个软件包)协同工作。