Web / Apache Axiom Interview questions
How do you use StAXOMBuilder to parse XML?
StAXOMBuilder wraps a StAX XMLStreamReader and drives Axiom's deferred building process as your code navigates the resulting tree.
XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader reader = xif.createXMLStreamReader(inputStream); StAXOMBuilder builder = new StAXOMBuilder(reader); OMElement root = builder.getDocumentElement(); // navigating triggers the builder to pull more StAX events as needed OMElement firstChild = root.getFirstElement();
Note that root here isn't necessarily fully built the moment getDocumentElement() returns - only the root's start tag has actually been parsed; the rest of the tree materializes lazily as you keep navigating.
In current Axiom versions, using OMXMLBuilderFactory.createStAXOMBuilder(reader) instead of the constructor directly is the recommended entry point, since it centralizes builder-creation logic and configuration in one place.
More Related questions...