Junior
What XML writing methods do you know?
sobes.tech AI
Answer from AI
DOM, SAX, StAX, JAXB.
DOM (Document Object Model):
Represents an XML document as a tree of objects in memory.
- Easy to use for small documents.
- Allows modification of the document structure.
- Requires loading the entire document into memory, which can be inefficient for large files.
// Example of DOM parsing
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
// ... inside a method
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("file.xml");
Element root = document.getDocumentElement();
NodeList nodes = root.getChildNodes();
// ... further processing
SAX (Simple API for XML):
Event-driven parser. Processes the document as it reads, calling handler methods upon encountering elements, attributes, etc.
- Efficient for large documents as it does not require loading the entire file into memory.
- More complex to use than DOM.
- Cannot modify the document.
// Example of SAX parsing
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
// ... inside a method
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
// Override methods to handle events
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
// Handle start of element
}
// ... other methods
};
saxParser.parse("file.xml", handler);
StAX (Streaming API for XML):
A cursor-based parser. The programmer controls the reading stream of the XML document.
- A compromise between DOM and SAX.
- More controllable than SAX.
- Efficient in memory for large documents.
// Example of StAX parsing
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
import java.io.FileReader;
// ... inside a method
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(new FileReader("file.xml"));
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLEvent.START_ELEMENT) {
// Handle start of element
}
// ... other events
}
reader.close();
JAXB (Java Architecture for XML Binding):
Provides mechanisms to bind Java classes to XML schema. Allows marshalling (converting Java objects to XML) and unmarshalling (converting XML to Java objects).
- Convenient for working with XML if a schema is available.
- Requires annotating Java classes or having Externalized Mapping.
// Example of JAXB marshalling
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
// ... class with JAXB annotations
// @XmlRootElement
// public class MyObject { ... }
// ... inside a method
JAXBContext context = JAXBContext.newInstance(MyObject.class);
Marshaller marshaller = context.createMarshaller();
StringWriter writer = new StringWriter();
MyObject obj = new MyObject(); // Your Java object
marshaller.marshal(obj, writer);
String xmlString = writer.toString();
// ...
| Method | Representation | Memory | Programming Model |
|---|---|---|---|
| DOM | Object tree | High | Object-oriented |
| SAX | Event stream | Low | Event-driven |
| StAX | Stream iterator | Low | Iterator-based |
| JAXB | Binding to Java objects | Depends | Binding-oriented |