XML and HTML processing
29 questions in 10 subtopics.
DOM parsing
Using the JDK DOM API, parse this file and print each book title with its price as aBigDecimal: <catalog><book id="1"><title>Java</title><price>39.90</price></book><book id="2"><title>SQL</title><price>29.50</price></book></catalog>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?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
Write a SAX handler that counts <order> elements and sums the text of their <amount> child elements. What pitfall does the characters callback have?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?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
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.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
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?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?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
Usingjavax.xml.xpath, select all titles of books with price greater than 30 from a parsed DOM Document, and return them as a List<String>.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?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?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
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?Write aNamespaceContext 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
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.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
javax.xml.bind (JAXB) code that compiled on Java 8 fails on Java 17. Why, and what replaces it today?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?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.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
What is an XXE attack? Show the kind of input that exploits it and how to configureDocumentBuilderFactory to prevent it.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?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
Using jsoup, fetch nothing remotely but parse an HTML string, extract all links with their absolute URLs from <a> tags insidediv.article, and explain how base URLs work.Your service accepts rich-text HTML from users and shows it to others. How would you sanitise it with jsoup, and what remains risky?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?No question matches these choices.