Scrivio Interview library

XML and HTML processing

29 questions in 10 subtopics.

DOM parsing

CodingFoundations7 min read
Using the JDK DOM API, parse this file and print each book title with its price as a BigDecimal: <catalog><book id="1"><title>Java</title><price>39.90</price></book><book id="2"><title>SQL</title><price>29.50</price></book></catalog>
DebuggingFoundations6 min read
Code iterates doc.getDocumentElement().getChildNodes() and casts every node to Element, failing with ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to org.w3c.dom.Element. Why?
ScenarioIntermediate7 min read
A batch job loads 800 MB XML files with DOM and fails with OutOfMemoryError on a 4 GB heap. Explain the memory cost of DOM and what you would change.

SAX parsing

CodingFoundations6 min read
Write a SAX handler that counts <order> elements and sums the text of their <amount> child elements. What pitfall does the characters callback have?
DesignIntermediate6 min read
When would you still choose SAX over StAX for a new Java component, and how do you stop parsing early once you have found what you need?
LeadershipPrincipal7 min read
Your team maintains a legacy integration that streams huge partner XML files with SAX handlers written 15 years ago by people who have left. The code works but nobody dares touch it. How do you manage this risk?

StAX parsing

CodingFoundations6 min read
Use XMLStreamReader to read a huge file of <customer id=".."><name>..</name></customer> records and pass each as a Customer record to a consumer without loading the whole file.
InternalsIntermediate7 min read
Compare XMLStreamReader and XMLEventReader in StAX, and explain how you would write a large XML file with XMLStreamWriter while keeping memory flat.

DOM vs SAX vs StAX

ConceptFoundations6 min read
Compare DOM, SAX and StAX on memory use, programming model, ability to modify the document and random access. Which would you pick for a 2 KB config file and for a 5 GB data export?
LeadershipAdvanced5 min read
Your organisation has XML integrations built over 20 years with DOM, SAX, StAX, JAXB, XStream and hand-written string concatenation. How would you standardise XML handling across teams?
LeadershipPrincipal5 min read
XML dominated enterprise integration in the 2000s (SOAP, WS-*, XSLT pipelines) and JSON now dominates. What does that history mean for how a large company should treat its remaining XML systems over the next decade?

XPath queries

CodingFoundations6 min read
Using javax.xml.xpath, select all titles of books with price greater than 30 from a parsed DOM Document, and return them as a List<String>.
DebuggingIntermediate6 min read
An XPath expression //order/id returns nothing for a document whose root is <orders xmlns="urn:shop:v2">, although it worked for the v1 file without a namespace. Why, and how do you fix it in Java?
ScenarioAdvanced7 min read
A rules engine stores thousands of user-provided XPath expressions applied to partner documents. CPU spikes and some expressions take seconds. How would you make this safe and fast?
LeadershipPrincipal6 min read
Many of your company's business rules are encoded in XPath and XSLT inside integration configurations that only two engineers understand. How do you reduce this organisational risk?

XML namespaces

ConceptFoundations6 min read
What problem do XML namespaces solve, and what is the difference between a prefix, a namespace URI and a default namespace? Are attributes in the default namespace?
CodingIntermediate6 min read
Write a NamespaceContext implementation for XPath queries over SOAP 1.2 messages (prefix soap for http://www.w3.org/2003/05/soap-envelope, prefix o for urn:orders), and explain the methods you must implement.

JDOM2

CodingFoundations6 min read
Using JDOM2, read a file, change the price of the book with id 2 to 25.00 and write the document back with pretty formatting.
LeadershipAdvanced6 min read
Several services use JDOM2 or dom4j for XML handling. Both are third-party libraries with slower release activity than the JDK. How do you decide whether to keep them or migrate?

Writing and transforming XML

ConceptFoundations6 min read
javax.xml.bind (JAXB) code that compiled on Java 8 fails on Java 17. Why, and what replaces it today?
CodingFoundations6 min read
Serialise a DOM Document to a String with indentation and UTF-8 encoding using the JDK Transformer API. What is a common surprise with indentation?
DesignIntermediate7 min read
Partners need the same order data in two XML formats and a CSV. Would you use XSLT, a binding library such as Jakarta XML Binding, or hand-written StAX writers? Explain the trade-offs.
DesignPrincipal6 min read
Your company must produce regulated XML reports (for example ISO 20022 payment messages) for many countries, with schema versions changing every year. How would you design the platform for producing and validating them?

XML security and XXE

ConceptFoundations6 min read
What is an XXE attack? Show the kind of input that exploits it and how to configure DocumentBuilderFactory to prevent it.
DebuggingIntermediate7 min read
A security scanner reports XXE in a Spring service, but developers say they never parse XML. Where could XML parsing be happening and how do you find and fix it?
LeadershipAdvanced8 min read
After an XXE incident in one service, you must make sure no service in a 300-service Java estate is vulnerable. How do you organise this?

HTML parsing with jsoup

CodingFoundations7 min read
Using jsoup, fetch nothing remotely but parse an HTML string, extract all links with their absolute URLs from <a> tags inside div.article, and explain how base URLs work.
ScenarioIntermediate7 min read
Your service accepts rich-text HTML from users and shows it to others. How would you sanitise it with jsoup, and what remains risky?
DesignAdvanced7 min read
Product wants a service that extracts structured data (title, price, availability) from thousands of retailer HTML pages with jsoup. What design and governance issues do you raise?