首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用xsom\dom\jaxb获取xsd的最大深度?

如何使用xsom\dom\jaxb获取xsd的最大深度?
EN

Stack Overflow用户
提问于 2012-04-26 01:34:38
回答 1查看 2.2K关注 0票数 2

如何使用xsom获取xsd的最大深度。

例如: xsd的每个复杂类型下的元素总数?

此外,如果在该复杂类型下存在as复杂类型,则that......using dom\xsom\jaxb下的元素+属性的数量

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:element name="root" type="root">
    <xs:annotation>
        <xs:documentation>
            Comment describing your root element
        </xs:documentation>
    </xs:annotation>
 </xs:element>

 <xs:complexType name="root">
    <xs:sequence>
        <xs:element name="element_count" type="xs:string"></xs:element>
        <xs:element name="employee" type="employee" maxOccurs="unbounded" minOccurs="0"></xs:element>
    </xs:sequence>
 </xs:complexType>

 <xs:complexType name="employee">
    <xs:sequence>
        <xs:element name="name" type="xs:string"></xs:element>
        <xs:element name="ID" type="xs:string"></xs:element>
        <xs:element name="Addresses" type="addresses" maxOccurs="1" minOccurs="0"></xs:element>
    </xs:sequence>
 </xs:complexType>

 <xs:complexType name="addresses">
    <xs:sequence>
        <xs:element name="address" type="address" maxOccurs="unbounded" minOccurs="0"></xs:element>
    </xs:sequence>
 </xs:complexType>

 <xs:complexType name="address">
    <xs:sequence>
        <xs:element name="line1" type="xs:string"></xs:element>
        <xs:element name="line2" type="xs:string"></xs:element>
        <xs:element name="city" type="xs:string"></xs:element>
        <xs:element name="type" type="xs:string"></xs:element>
    </xs:sequence>
 </xs:complexType>
</xs:schema>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-08 01:46:21

我其实就是在找这个。我在API中找不到任何东西,所以昨天用递归找到了一种方法。我只是简单地粘贴我的递归方式来读取最深的内容,并将它们添加到Hashmap中。

代码语言:javascript
复制
/*
 * Parses the xml schema string into a hashmap
 * note that hashmap has a form of a tree
 */
public HashMap<String, Object> getXmlElements(InputStream xml) {
    //---
    XSOMParser parser = new XSOMParser();
    //---
    try{
        parser.parse(xml);
    } catch(Exception ex){
        logger.fatal("Could not parse the inputstream: " + ex);
    }
    //---
    XSSchemaSet schemaSet = null;
    try {
        schemaSet = parser.getResult();
    } catch (SAXException ex) {
        logger.fatal("Could not parse: " + ex);
    }
    //---
    HashMap<String, Object> hmReturned = new HashMap<String, Object>();
    HashMap<String, Object> hm = new HashMap<String, Object>();
    Iterator <XSElementDecl> itre = schemaSet.iterateElementDecls();
    //---
    while(itre.hasNext()) {
        XSElementDecl xse = (XSElementDecl) itre.next();
        hmReturned.put(xse.getName(), hm);
        XSComplexType xscomp = xse.getType().asComplexType();
        if (xscomp != null) {
            XSContentType xscont = xscomp.getContentType();
            XSParticle particle = xscont.asParticle();
            getElementsRecursively(hm,  particle);
        }
    } 
    //---
    return hmReturned;
}

/*
 * recursive helper method of getXmlElements
 * note that since we don't know the "deepness" of the
 * schema a recursive way of implementation was necessary
 */
private void getElementsRecursively(HashMap<String, Object> hm, XSParticle xsp) {
     if(xsp != null){
         XSTerm term = xsp.getTerm();
         if(term.isElementDecl()) {
             XSComplexType xscmp =  (term.asElementDecl()).getType().asComplexType();
             //---
             if (xscmp == null){
                 if(xsp.getMinOccurs() == 0)
                     hm.put(term.asElementDecl().getName(), "|");
                 else
                     hm.put(term.asElementDecl().getName(), "=");
             } else{
                 XSContentType xscont = xscmp.getContentType();
                 XSParticle particle = xscont.asParticle();
                 HashMap<String, Object> newHm = new HashMap<String, Object>();
                 getElementsRecursively(newHm, particle);
                 hm.put(term.asElementDecl().getName(), newHm);
             }
             //---
         } else if(term.isModelGroup()){
             XSModelGroup model = term.asModelGroup();
             XSParticle[] parr = model.getChildren();
             for(XSParticle partemp : parr ){
               getElementsRecursively(hm, partemp);
             }
         }
     }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10320814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档