如何使用xsom \ dom \ jaxb获得最大深度的xsd?
问题描述:
如何使用xsom获得最大深度的xsd。如何使用xsom dom jaxb获得最大深度的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);
}
}
}
}
你的溶液具有小的错误:线'的HashMap HM =新的HashMap ();'从方法'getXmlElements'应该是'while'块内。 – 2012-12-15 18:26:33
'xsp.getMinOccurs()== 0'应该替换为'xsp.getMinOccurs()。intValue()== 0',因为该方法返回一个'BigInteger'。 – 2012-12-15 18:27:39