gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:54:23 25 4
gpt4 key购买 nike

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

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

此外,如果在该复杂类型下存在复杂类型,那么该复杂类型下的元素+属性的数量……使用 dom\xsom\jaxb

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

最佳答案

其实我只是在找这个。我在 API 中找不到任何东西,所以昨天找到了一种递归方法。我只是将我的递归方式粘贴到最深处并将它们添加到一个 Hashmap 中。

/*
* 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);
}
}
}
}

关于java - 如何使用 xsom\dom\jaxb 获取 xsd 的最大深度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10320814/

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