0

我想找出使用​​ java 的 xsom 的 xsd 元素的最小出现次数 maximm 出现次数。我得到了这段代码来找出复杂的元素。谁能帮我找出所有 xsd 元素的出现次数。至少给我一个代码片段用于查找事件的类和方法

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());
    }
  }
}
4

1 回答 1

1

假设您指的是 com.sun.xml.xsom,则该事件是特定于粒子的(元素不是唯一的粒子)。

以下是 API:maxOccursminOccurs

如需了解如何使用 XSOM 遍历模式树的来源,请查看此处。它基本上显示了访问者模式如何与 XSOM(Sun 为其构建了一个包)一起工作。

于 2012-02-15T20:15:31.243 回答