I am using the Apache XmlSchema Core
library to parse the XSD
file and get all the elements and its children type (datatype, maxOccurs, etc). I am following the documentation Apache XML SCHEMA CORE and trying to do. But after navigating to a certain point I am getting a bit confused. Can someone please guide me on how can I traverse through my XSD
file and get all the elements and its child elements along with the related information?
I am able to get all the XSD information in my schema
element I just want to know how can I access the child elements from my root RootFood
and get its related information. Any help would be really appreciated.
I tried to continue further and this is what I have so far:
The element RootFood
belongs to the instance of the class XmlSchemaGroupParticle
. I tried to debug the code to find the elements that are associated with my rootParticles
, it has the field called items
within which I have my food
element items->[0]->namedDelegate->qName->localPart
but when I try to add GET
method on rootParticles
to obtain the items
then there is no such method.
The XmlSchemaParticle
extends following classes: XmlSchemaAnnotated
, XmlSchemaObject
, and implements the interface XmlSchemaObjectBase
but none of them have the field called Items
.
Following is the Java code I have so far, I tried several things:
public class XMLSchemaCore {
public static void main(String[] args) throws URISyntaxException,
FileNotFoundException,
UnsupportedEncodingException {
String xsdPath = Paths.get(XMLSchemaCore.class.getClassLoader().getResource("test.xsd").toURI()).toFile().getAbsolutePath();
String filePath = Path.of(xsdPath).toString();
InputStream is = new FileInputStream(filePath);
XmlSchemaCollection schemaCol = new XmlSchemaCollection();
// Schema contain the complete XSD content which needs to be parsed
XmlSchema schema = schemaCol.read(new StreamSource(is));
// schema.write(System.out);
for (Map.Entry<QName, XmlSchemaElement> entry: schema.getElements().entrySet()) {
// Root element and its contents
QName parentElementName = entry.getKey();
XmlSchemaElement parentElementValues = entry.getValue();
// Get the elements based on the Root element
XmlSchemaElement root = schema.getElementByName(parentElementName);
// Get the type of the root element
XmlSchemaType type = root != null ? root.getSchemaType() : null;
// Check if the root element is of Complex type
if (type instanceof XmlSchemaComplexType) {
// Get the Particles associated with the element
XmlSchemaParticle rootParticles = ((XmlSchemaComplexType) type).getParticle();
// Check particle belongs to which type
if (rootParticles instanceof XmlSchemaAny) {
System.out.println("Any Schema Type");
} else if (rootParticles instanceof XmlSchemaElement) {
System.out.println("Element Schema Type");
} else if (rootParticles instanceof XmlSchemaGroupParticle) {
System.out.println("Group Schema Type");
System.out.println(rootParticles);
System.out.println(rootParticles);
} else if (rootParticles instanceof XmlSchemaGroupRef) {
System.out.println("Group Ref Schema Type");
}
}
}
}
}
Following is the XSD
file I have:
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="RootFood">
<xs:complexType>
<xs:sequence>
<xs:element name="food">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name" />
<xs:element type="xs:string" name="price" />
<xs:element type="xs:string" name="description" />
<xs:element type="xs:short" name="calories" />
<xs:element name="ingredients">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="ingredient"
maxOccurs="unbounded" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>