import java.io.IOException;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XMLParser {
private static final String URL = "query.xml";
private static final String ROOT_NAME = "queries";
private static Logger logger = Logger.getLogger(XMLParser.class.getName());
public static void main(String[] args) {
Document document;
Node root;
NodeList queries;
Node query;
Element element;
try {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(URL);
document.getDocumentElement().normalize();
root = document.getElementsByTagName(ROOT_NAME).item(0);
queries = root.getChildNodes();
for (int i = 0; i < queries.getLength(); i++) {
query = queries.item(i);
if (query.getNodeType() == Node.ELEMENT_NODE) {
element = (Element) query;
System.out.println(element.getAttribute("id"));
System.out.println(element.getTextContent().trim());
}
}
} catch (SAXException e) {
logger.warning(e.toString());
} catch (IOException e) {
logger.warning(e.toString());
} catch (ParserConfigurationException e) {
logger.warning(e.toString());
}
}
}