XML (eXtensible Markup Language) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The design goals of XML emphasize simplicity, generality, and usability across the Internet. It is primarily used to facilitate the sharing of structured data across different information systems, particularly via the Internet, and is used both to encode documents and to serialize data.
XML data is organized in a tree-like structure and is made up of several key components:
<?xml version="1.0" encoding="UTF-8"?>
<company>
<employee id="1">
<name>John Doe</name>
<email>john.doe@example.com</email>
<department>Human Resources</department>
</employee>
<employee id="2">
<name>Jane Smith</name>
<email>jane.smith@example.com</email>
<department>Finance</department>
</employee>
</company>
Java provides several APIs to work with XML data, including DOM, SAX, and StAX. Below are brief explanations and example usages of these APIs:
Example Usage:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class XmlExample {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("company.xml"));
NodeList employees = document.getElementsByTagName("employee");
for (int i = 0; i < employees.getLength(); i++) {
Element employee = (Element) employees.item(i);
String name = employee.getElementsByTagName("name").item(0).getTextContent();
System.out.println("Employee Name: " + name);
}
}
}
Example Usage:
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
public class SaxExample extends DefaultHandler {
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("name")) {
System.out.println("Found an Employee Name");
}
}
public void characters(char ch[], int start, int length) throws SAXException {
System.out.println("Name: " + new String(ch, start, length));
}
}
Example Usage:
import javax.xml.stream.*;
public class StaxExample {
public static void main(String[] args) throws XMLStreamException {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(new FileInputStream("company.xml"));
while(reader.hasNext()) {
if (reader.next() == XMLStreamConstants.START_ELEMENT) {
if (reader.getLocalName().equals("name")) {
System.out.println("Employee Name: " + reader.getElementText());
}
}
}
}
}
XML remains a powerful tool for data interchange and configuration due to its flexibility, extensive support across programming environments, and widespread use in legacy systems.