Skip to content

Instantly share code, notes, and snippets.

@helderdarocha
Created February 3, 2014 20:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save helderdarocha/8791651 to your computer and use it in GitHub Desktop.
Save helderdarocha/8791651 to your computer and use it in GitHub Desktop.
Example processing a simple XML schema in SAX and generating a tree
public class SAXReaderExample {
public static final String PATH = "/tmp/resources";
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader reader = sp.getXMLReader();
reader.setContentHandler(new SchemaSaxHandler());
reader.parse(new InputSource(new FileInputStream(new File(PATH, "source.xsd"))));
}
}
class SchemaElement {
private String name;
private String type;
private List<SchemaElement> children;
private Map<String, String> attributes;
// getters and setters
}
class SchemaComplexType {
private String name;
private List<SchemaElement> children;
private Map<String, String> attributes = new HashMap<>();
// getters and setters + addAttribute
}
class SchemaSaxHandler extends DefaultHandler {
// temporary - always null when tag closes
private String currentSimpleTypeName;
private String currentSimpleTypeBaseType;
private SchemaElement currentElement;
private SchemaComplexType currentComplexType;
private List<SchemaElement> currentSequence;
// cumulative - will use the data when XML finishes
private Map<String, String> simpleTypes = new HashMap<>();
private Map<String, SchemaComplexType> complexTypes = new HashMap<>();
private SchemaElement rootElement;
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (qName.equals("xs:simpleType")) {
currentSimpleTypeName = atts.getValue("name");
}
if (qName.equals("xs:restriction")) {
currentSimpleTypeBaseType = atts.getValue("base");
}
if (qName.equals("xs:complexType")) {
currentComplexType = new SchemaComplexType();
currentComplexType.setName(atts.getValue("name"));
}
if (qName.equals("xs:sequence")) {
currentSequence = new ArrayList<>();
}
if (qName.equals("xs:element")) {
currentElement = new SchemaElement();
currentElement.setName(atts.getValue("name"));
currentElement.setType(atts.getValue("type"));
if (currentSequence != null) {
currentSequence.add(currentElement);
} else {
rootElement = currentElement;
}
}
if (qName.equals("xs:attribute")) {
currentComplexType.addAttribute(atts.getValue("name"), atts.getValue("type"));
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("xs:simpleType")) {
simpleTypes.put(currentSimpleTypeName, currentSimpleTypeBaseType);
currentSimpleTypeName = null;
currentSimpleTypeBaseType = null;
}
if (qName.equals("xs:complexType")) {
complexTypes.put(currentComplexType.getName(), currentComplexType);
currentComplexType = null;
}
if (qName.equals("xs:sequence")) {
if (currentComplexType != null) {
currentComplexType.setChildren(currentSequence);
}
currentSequence = null;
}
}
@Override
public void endDocument() throws SAXException {
makeTree(rootElement);
printTree(rootElement, "");
}
public void makeTree(SchemaElement element) {
SchemaComplexType type = complexTypes.get(element.getType());
if (type != null) {
List<SchemaElement> children = type.getChildren();
element.setChildren(children);
for (SchemaElement child : children) {
makeTree(child);
}
element.setAttributes(type.getAttributes());
} else {
element.setType(simpleTypes.get(element.getType()));
}
}
private void printTree(SchemaElement element, String indent) {
System.out.println(indent + element.getName() + " : " + element.getType());
Map<String, String> attributes = element.getAttributes();
if (attributes != null) {
for (Map.Entry<String, String> entry : attributes.entrySet()) {
System.out.println(" @" + entry.getKey() + " : " + simpleTypes.get(entry.getValue()));
}
}
List<SchemaElement> children = element.getChildren();
if (children != null) {
for (SchemaElement child : children) {
printTree(child, indent + " ");
}
}
}
}
@bilelneb
Copy link

Hey .. i am really interested in parsing XML Schema the way you did .. but it didn't gave me the result obtained with the example here https://stackoverflow.com/questions/21527978/parsing-xsd-to-tree-structure .. i want to know why ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment