XML Parser Implementation in Java
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓XML Parser runs in O(n) time and O(n) space, in Java as in every other language.
- ✓JAXP is in the JDK and offers all three shapes: DocumentBuilderFactory for a DOM, SAXParserFactory for push parsing, and XMLInputFactory for StAX pull parsing.
- ✓Prefer StAX for large documents and DOM only when random access is genuinely required. Whichever you choose, configure the factory explicitly before the first parse, because the defaults are the problem.
- ✓The Java trap: Java is the language where external entity expansion has caused the most damage, because DocumentBuilderFactory historically resolved external entities by default, which turns any service that parses user-supplied XML into a file-disclosure and server-side request primitive.
- ✓A DOM of a large document commonly occupies several times the document size in heap because every node is an object.
What XML Parser actually is
XML Parser in Java: what you actually reach for
Java implementation
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import java.io.StringReader;
public final class SafeXml {
/** DOM, configured against XXE. The DEFAULTS are the vulnerability. */
public static DocumentBuilderFactory hardenedDomFactory() throws Exception {
var f = DocumentBuilderFactory.newInstance();
// The single most effective switch: no DTD at all, no entities.
f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
f.setFeature("http://xml.org/sax/features/external-general-entities", false);
f.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
f.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
f.setXIncludeAware(false);
f.setExpandEntityReferences(false);
return f;
// NOTE: these settings apply to THIS factory only. A SAXParserFactory
// or XMLInputFactory elsewhere in the app needs its own hardening.
}
/** StAX pull parsing: no tree, flat memory, and hardened separately. */
public static int countOrders(String xml) throws Exception {
var factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
factory.setProperty("javax.xml.stream.isSupportingExternalEntities", false);
XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(xml));
int count = 0;
while (reader.hasNext()) {
if (reader.next() == XMLStreamReader.START_ELEMENT
&& "order".equals(reader.getLocalName())) {
count++;
}
}
reader.close();
return count;
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Java-specific trap
Performance in Java, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for XML Parser?
JAXP is in the JDK and offers all three shapes: DocumentBuilderFactory for a DOM, SAXParserFactory for push parsing, and XMLInputFactory for StAX pull parsing.
What is the most common Java mistake when implementing XML Parser?
Java is the language where external entity expansion has caused the most damage, because DocumentBuilderFactory historically resolved external entities by default, which turns any service that parses user-supplied XML into a file-disclosure and server-side request primitive. The single most effective mitigation is disabling the document type declaration entirely with the disallow-doctype-decl feature, and OWASP publishes a per-factory cheat sheet precisely because each of the three APIs needs its own set of switches: setting them on DocumentBuilderFactory does nothing for the SAXParserFactory used elsewhere in the same application. Newer JDK versions and the FEATURE_SECURE_PROCESSING flag improve the baseline but do not universally disable DTD processing, so the explicit configuration is still the correct habit.
How does XML Parser actually perform in Java?
A DOM of a large document commonly occupies several times the document size in heap because every node is an object. StAX reads forward with no tree and is the right default for anything measured in megabytes. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialMost AI and dev tools have a student plan. See which ones and how to claim them on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.