gpt4 book ai didi

java - 如何使用 xsom 查找 xsd 中元素的最小-最大出现次数

转载 作者:行者123 更新时间:2023-12-01 15:33:15 27 4
gpt4 key购买 nike

我想使用java的xsom找出xsd元素的最小出现次数和最大出现次数。我得到了这个代码来找出复杂的元素。任何人都可以帮我找出所有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());
}
}
}

最佳答案

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

以下是 API:maxOccursminOccurs

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

关于java - 如何使用 xsom 查找 xsd 中元素的最小-最大出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9298710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com