Quick revision
The short answer
I would first make the system safe to touch: characterisation tests built from real, anonymised partner files, and a written description of the handler's state machine and the business rules it encodes. Then I would decide on evidence whether to keep and harden it, refactor it to StAX, or replace it with a platform, based on how often it changes and what a failure costs. Secure parser settings and current JDK support are non-negotiable whatever we choose, and I would spread the knowledge so it never again depends on one person.
What to remember
In this answer, we treated the legacy SAX integration as a risk to manage rather than code to fear. We built characterisation tests from real anonymised partner files, documented the handler's state machine and business rules, and then chose between keeping and hardening, refactoring to StAX, or replacing with a platform based on change frequency and failure cost. Secure parser configuration and a supported JDK are required in every case, and pairing, rotation and monitoring make sure the system never again depends on a single person.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would first make the system safe to touch: characterisation tests built from real, anonymised partner files, and a written description of the handler's state machine and the business rules it encodes. Then I would decide on evidence whether to keep and harden it, refactor it to StAX, or replace it with a platform, based on how often it changes and what a failure costs. Secure parser settings and current JDK support are non-negotiable whatever we choose, and I would spread the knowledge so it never again depends on one person.
Why this question is asked
Every long-lived company has a system like this: it moves money or orders every night, it was written by people who left years ago, and the team treats it like an unexploded bomb. Fear is itself a risk, because the day a partner changes their format or a JDK upgrade forces a change, nobody can act quickly. Interviewers ask this at senior levels to see whether we can turn an emotional problem into a managed one, with steps, costs and owners. In this answer, we will look at why this code is risky, build a safety net with characterisation tests, recover the knowledge, decide between keep, refactor and replace, cover the security and platform baseline, and make sure the knowledge is shared.
1Why Fifteen-Year-Old SAX Handlers Are Risky
A SAX handler, the callback object the parser calls for every tag and piece of text, keeps its context in fields: flags such as inInvoice, counters, and buffers. Over fifteen years, each new partner quirk tends to add another flag and another if statement. The result is an implicit state machine, a set of states and transitions that exists only in the code, and the business rules are buried inside it: which amounts are summed, which records are skipped, how a missing currency defaults.
The code works, which is valuable. The risk is that nobody knows which behaviours are deliberate and which are accidents, so every change could break a rule the business depends on. The goal is not to rewrite it quickly; it is to make change boring again.
2Characterisation Tests From Real Files
A characterisation test does not assert what the code should do; it records what the code does today, so we notice any change. We take a sample of real partner files, anonymise them (replace names, account numbers and addresses while keeping structure, lengths and odd characters), run them through the current system and save the outputs as golden files. In CI, every build reruns the files and compares the outputs byte for byte, or field by field if ordering is not significant.
Choosing the samples matters more than the number. We want one file per partner, plus the strange ones: the file with a BOM, the one with a CDATA section, the empty batch, the huge batch, the one that was manually fixed last year. Production logs and support tickets tell us which inputs exist. With this net in place, the team can change the code and know within minutes whether behaviour moved.
3Recover the Knowledge
Next we write down what the code encodes. A simple, effective format is a state diagram of the handler, which states it can be in and which tags move it between them, plus a table of business rules with the line of code that implements each one and a sample file that exercises it. Reading the code with a domain expert from operations or finance often reveals rules that are still needed and rules for partners who left in 2014.
This document is also how we size the work. A handler with 6 states and 12 rules is a weekend refactor; one with 40 flags and 150 rules is a project and needs a plan.
4Keep, Refactor or Replace
With tests and documentation, the decision becomes a normal engineering choice. The two questions that drive it are how often the code needs to change and what a failure costs.
- Characterisation tests and rule catalogue in placeprecondition for any option
- Changes needed often?YesConsider refactor or replaceNoKeep: patch and harden
- Is XML parsing our differentiator?YesRefactor to StAX in small stepsNoEvaluate an integration platform
- Run old and new side by sidecompare outputs on real traffic
- Switch over, keep the old path for rollbackone partner at a time
5 steps in order. Play them, or step through them one at a time.
Keeping is a valid decision when change is rare; it must still be made safe.
If partners change formats once every few years, keeping the code is usually right: we harden it, add the tests and documentation, and leave the structure alone. If changes come every month and each takes a week of fear, refactoring pays back. Converting a handler to StAX, where our own loop pulls events and state lives in local variables and small methods, often turns a tangle of flags into readable code, and we can do it one message type at a time with the golden files proving equivalence. Replacing the code with a vendor product or an integration platform makes sense when many partners and formats are involved and the company already runs such a platform, but it brings licence cost and its own migration risk.
Whatever we choose, we run old and new paths in parallel on real traffic for a while and compare their outputs before switching, which turns a big-bang cutover into a series of small, reversible steps.
5A Security and Platform Baseline
Old SAX code usually creates its parser with default settings, which may allow DTDs and external entities, the door for XML external entity (XXE) attacks. Regardless of the larger decision, we set disallow-doctype-decl and secure processing on the SAXParserFactory, and we check that the code runs on a supported JDK. Fifteen-year-old code sometimes imports internal classes such as com.sun.org.apache.xerces.internal or relies on removed APIs, which blocks JDK upgrades. Fixing this is cheap with the characterisation tests in place and removes the most likely forced change.
6Spread the Knowledge
The root cause was a system that lived in a few heads. We fix it deliberately: pair two engineers on every change to this area, rotate the on-call ownership through the team, and review the rule catalogue whenever a partner changes. A useful target is that at least three people can make and release a change to the integration without help. We also add monitoring, such as record counts per file and rejected records, so problems show up as alerts rather than calls from partners.
7Follow-up questions
What if we cannot get real partner files because of privacy rules?
We write an anonymiser that walks the files with a streaming parser and replaces sensitive values with fake ones of the same length and character class, keeping structure and edge cases. The anonymiser itself is reviewed by the data protection team. If even that is not allowed, we build synthetic files from the formats' specifications and production statistics, but real-shaped data is far better at catching quirks.
How do you convince management to fund work on code that works?
I frame it as risk with a price: what an outage of this integration costs per day, how long the last change took and why, and what the JDK end-of-support date means for us. The first steps, tests and documentation, are cheap and reduce risk immediately, so they are easy to approve; the larger refactor or replacement decision comes later with better data.
Would you rewrite it in a new language or framework?
Not as a goal in itself. A rewrite throws away fifteen years of encoded fixes, and the characterisation tests would have to be met exactly anyway. I would only replace the code when the change rate or the platform strategy clearly justifies it, and even then migrate partner by partner.
8Conclusion
In this answer, we treated the legacy SAX integration as a risk to manage rather than code to fear. We built characterisation tests from real anonymised partner files, documented the handler's state machine and business rules, and then chose between keeping and hardening, refactoring to StAX, or replacing with a platform based on change frequency and failure cost. Secure parser configuration and a supported JDK are required in every case, and pairing, rotation and monitoring make sure the system never again depends on a single person.
Open with
My first goal is to make the code safe to change, not to change it. I would collect real partner files, anonymise them while keeping their structure and quirks, run them through the current system and save the outputs as golden files, so CI tells us immediately if any behaviour moves.
Then, if they want more
In parallel, I would sit an engineer with someone from operations to document what the handlers really do: the state machine hidden in all those flags, and a catalogue of the business rules with the code and a sample file for each. With that, the decision becomes rational. If the format rarely changes, we keep the code and harden it.
If changes are frequent and slow, I would refactor towards StAX one message type at a time, where state lives in local variables instead of flags, using the golden files to prove equivalence. If we have many partners and already run an integration platform, replacement might make sense, but only partner by partner with parallel runs.
Regardless of the option, I would set secure parser features, disallow-doctype-decl in particular, and make sure it runs on our current JDK without internal APIs. Finally, I would pair and rotate people through this area until at least three engineers can change and release it without help.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your team maintains a legacy integration that streams huge partner XML files with SAX...": https://scrivio-interview-library.netlify.app/java/xml-processing/20402e3419/