Quick revision
The short answer
It is the abstract factory pattern: our code depends only on the abstract factory, DocumentBuilderFactory, and the abstract product, DocumentBuilder, while the concrete classes are chosen at run time. That design lets the XML parser be replaced, through a system property, a configuration file or a service provider, without changing application code. It also lets us configure the factory, for example namespace awareness and secure processing features, before it creates builders. Factories and builders are not guaranteed to be thread-safe, so we configure a factory once and then use it and its builders carefully.
What to remember
In this answer, we saw that DocumentBuilderFactory.newInstance() followed by newDocumentBuilder() is the abstract factory pattern: code depends on abstract factory and product types, and the concrete parser is found at run time through a system property, a configuration file, a service provider or the built-in default. That lets parsers be replaced without code changes and lets the factory carry configuration such as namespace awareness and XXE protection. Factories and builders are not guaranteed thread-safe, so we configure once and use builders per parse or per thread.
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.
It is the abstract factory pattern: our code depends only on the abstract factory, DocumentBuilderFactory, and the abstract product, DocumentBuilder, while the concrete classes are chosen at run time. That design lets the XML parser be replaced, through a system property, a configuration file or a service provider, without changing application code. It also lets us configure the factory, for example namespace awareness and secure processing features, before it creates builders. Factories and builders are not guaranteed to be thread-safe, so we configure a factory once and then use it and its builders carefully.
Why this question is asked
Parsing XML in Java takes two steps that look redundant at first: get a factory, then ask the factory for a builder, then parse. Understanding why the API is shaped this way explains a lot about JAXP, the Java API for XML Processing, and about a common security setting that every XML-parsing service should know. Interviewers ask it because the pattern is classic, the JDK example is real, and the follow-up about configuration and threads separates people who have used it in production. In this answer, we will read the two-factory line, name the pattern, see how newInstance() finds an implementation, configure the factory safely, and look at thread safety.
1Two Factories in One Line
Here is the usual code for reading an XML document into a DOM tree, the in-memory tree of elements and attributes.
Notice that our code never mentions the class that is printed. DocumentBuilderFactory and DocumentBuilder are abstract classes in javax.xml.parsers; the real ones belong to whichever parser implementation is in use, here the copy of Apache Xerces built into the JDK.
2It Is the Abstract Factory Pattern
The abstract factory pattern provides an interface for creating objects without naming their concrete classes, so client code depends only on abstract types. Here the abstract factory is DocumentBuilderFactory and the abstract product is DocumentBuilder. A parser vendor provides a concrete factory and a concrete builder that belong together, so a Xerces factory always produces a Xerces builder.
newInstance() is itself a static factory method that chooses the concrete factory. So the line combines two creational ideas: a static factory that picks an implementation, and an abstract factory that creates products consistent with that implementation.
- Application codeuses
DocumentBuilderFactoryandDocumentBuilderonly - Abstract factory
javax.xml.parsers.DocumentBuilderFactory - Abstract product
javax.xml.parsers.DocumentBuilder - Concrete factory, chosen at run timeJDK built-in Xerces or another provider
- Concrete productthe builder that belongs to that factory
5 layers, from the top down. Step through them one at a time.
Replacing the bottom two layers needs no change in the top one.
3How newInstance() Finds the Implementation
The reason for this design is replaceability. Years ago, applications often needed a newer or different parser than the one in the JDK, and they could not change every line that created a parser. So newInstance() looks for an implementation in a defined order: a system property named after the factory, javax.xml.parsers.DocumentBuilderFactory, then a JAXP configuration file in the JDK's conf directory, then a provider found through ServiceLoader on the class path, and finally the JDK's built-in default. The exact configuration files have changed in recent releases, so check your version's documentation.
So an application can switch parsers by adding a jar or setting a property, with no code change, which is exactly what the pattern promises. Java 9 also added newDefaultInstance(), which skips the lookup and always returns the built-in implementation, useful when we want predictability and no surprises from a jar on the class path.
4Configuring the Factory Before Creating Builders
The factory is also the place where configuration lives. Every builder it creates inherits its settings, so we configure once and then create builders. Two settings matter in almost every service: namespace awareness, which is off by default for historical reasons, and protection against XML external entity attacks, known as XXE, where a malicious document makes the parser read local files or call internal URLs.
Notice that the disallow-doctype-decl feature is identified by a Xerces URI. Features are implementation-specific, which is the one place where the abstraction leaks: a different provider might not support the same feature names and throw ParserConfigurationException. That is another reason many services prefer newDefaultInstance(), so the security settings are known to work.
5Thread Safety: Configure Once, Use Carefully
The JAXP documentation does not guarantee that a DocumentBuilderFactory is thread-safe, and a DocumentBuilder is not safe to use from several threads at once. So we should not configure a shared factory while other threads use it, and never share one builder across threads.
A common, safe approach is to create and fully configure the factory once at startup, and then either create a new builder per parse, which is fairly cheap compared with parsing, or keep one builder per thread with a ThreadLocal and call reset() between uses. If profiling shows newDocumentBuilder() as a cost, the per-thread builder is the usual optimisation. What we avoid is calling newInstance() for every document, which repeats the implementation lookup, and sharing mutable parser objects between threads.
6Follow-up questions
Why is DocumentBuilderFactory an abstract class rather than an interface?
It was designed in the Java 1.4 era, before default and static interface methods existed, and it needed static methods such as newInstance() plus shared configuration code. An abstract class could hold both. Newer JDK APIs would usually use an interface with static factory methods.
Is this the same as the factory method pattern?
Not quite. newInstance() is a static factory method, and newDocumentBuilder() is a creation method, but the overall structure, an abstract factory producing abstract products whose concrete classes come as a matched set from one provider, is the abstract factory pattern.
What is the danger of leaving XML parsing at its defaults?
Depending on the version and settings, the parser may resolve external entities in a DOCTYPE, which lets an attacker read files or make network requests from the server, or expand nested entities until memory runs out. Disallowing DOCTYPE declarations and enabling secure processing closes these doors for most services.
7Conclusion
In this answer, we saw that DocumentBuilderFactory.newInstance() followed by newDocumentBuilder() is the abstract factory pattern: code depends on abstract factory and product types, and the concrete parser is found at run time through a system property, a configuration file, a service provider or the built-in default. That lets parsers be replaced without code changes and lets the factory carry configuration such as namespace awareness and XXE protection. Factories and builders are not guaranteed thread-safe, so we configure once and use builders per parse or per thread.
Open with
This is the abstract factory pattern. My code only depends on two abstract types from javax.xml.parsers: DocumentBuilderFactory, the abstract factory, and DocumentBuilder, the abstract product. The concrete classes come as a matching pair from whichever parser implementation is in use, by default the copy of Xerces inside the JDK. newInstance is a static factory method that finds that implementation at run time: a system property, then the JAXP configuration file, then a service provider on the class path, then the built-in default.
Then, if they want more
The reason for the design is replaceability: years ago applications needed to swap parsers without touching code, and they could just add a jar or set a property. If I want to avoid that lookup, newDefaultInstance always gives the built-in one. The factory is also where configuration lives, so every builder inherits it.
In practice I always set namespace awareness, enable secure processing and disallow DOCTYPE declarations to block XXE attacks.
One caution: the factory is not guaranteed to be thread-safe and builders are not thread-safe at all, so I configure the factory once at startup and then create a builder per parse, or keep one per thread with a ThreadLocal and reset it between uses.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- 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 "DocumentBuilderFactory.newInstance() returns a factory, and the factory returns a...": https://scrivio-interview-library.netlify.app/java/creational-patterns/d1d7cb1c43/