XML interview question · Most asked
Which characters must be escaped in XML, and what is CDATA?
Summary
& and < must always be escaped (&, <) in text and attribute values; quotes must be escaped inside attribute values delimited by the same quote. CDATA sections (<![CDATA[ ... ]]>) let you write text literally; after parsing it's ordinary text, and CDATA can't contain ]]>.
Longer Answer
Named entities such as are HTML, not XML. The real lesson: never build XML by string concatenation; use a library that escapes for you.
Key Points
- & and < always.
- Quotes inside attributes.
- CDATA = literal text, same result after parsing.
- Use a builder, not string formatting.
Deep dive: XML Tutorial: Escaping, CDATA and Encoding
Explanation
What interviewers look for
Correct entities and the builder recommendation.
More most-asked XML questions
- Explain XML namespaces and the default namespace.
- Your XPath query returns nothing, but the elements are clearly there. What's the likely cause?
- Compare DOM, SAX and StAX (or pull parsing).
- What's the difference between well-formed and valid XML?
- What is an XXE attack and how do you prevent it?
- What is XML and where is it still used today?
- Compare DTD, XML Schema (XSD), RELAX NG and Schematron.
- Explain XPath axes and give examples of predicates.