<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package lecture_en;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * Przyklad zastosowania DOM.
 * 
 * @author Patryk Czarnik
 */
public class StaffDOM_HighLevel {
	public static void main(String[] args) {
		if (args.length &lt; 1) {
			System.out.println("Too few args");
			return;
		}

		int result = 0;
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(args[0]);

			Element root = doc.getDocumentElement();
			NodeList list = root.getElementsByTagName("person");
			for (int i = 0; i &lt; list.getLength(); ++i) {
				Element person = (Element) list.item(i);
				if ("specialist".equals(person.getAttribute("position")))
					result += processPerson(person);
			}
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (FactoryConfigurationError e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Result: " + result);
	}

	private static int processPerson(Element person) {
		NodeList list = person.getElementsByTagName("salary");
		if (list.getLength() &gt; 0) {
			String value = list.item(0).getTextContent();
			try {
				return Integer.parseInt(value);
			} catch (NumberFormatException e) {
				return 0;
			}
		}
		return 0;
	}
}
</pre></body></html>