Java tutorial
//package com.java2s; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class Main { /** * Extracts the element named <var>name</var> with the namespace * <var>elementNS</var> that is a descentant of the Document * <var>message</var>. This method returns null if the named elemens is not * a child of <var>message</var> or if <var>message</var> contains more than * one element named <var>name</var> * * @param message * Source Document that contains the element named * <var>name</var> * @param elementNS * Namespace of the element named <var>name</var> * @param name * Name of the element to be extracted * @return Element named <var>name</var>, <br> * or null if <var>message</var> contains zero or more than one * element named <var>name</var> */ public static Element extractElement(Document message, String elementNS, String name) { return extractElement(message, elementNS, name, true); } /** * Extracts the element named <var>name</var> with the namespace * <var>elementNS</var> that is a descentant of the Document * <var>message</var>. This method returns null if the named elemens is not * a child of <var>message</var> or if <var>message</var> contains more than * one element named <var>name</var> * * @param message * Source Document that contains the element named * <var>name</var> * @param elementNS * Namespace of the element named <var>name</var> * @param name * Name of the element to be extracted * @return Element named <var>name</var>, <br> * or null if <var>message</var> contains zero or more than one * element named <var>name</var> */ public static Element extractElement(Document message, String elementNS, String name, boolean strict) { NodeList list = message.getElementsByTagNameNS(elementNS, name); if (list == null || list.getLength() == 0 || (strict && list.getLength() > 1)) return null; Element element = (Element) list.item(0); return element; } /** * Extracts the element named <var>name</var> with the namespace * <var>elementNS</var> that is a descentant of the element * <var>message</var>. This method returns null if the named elemens is not * a child of <var>message</var> or if <var>message</var> contains more than * one element named <var>name</var> * * @param message * Source Document that contains the element named * <var>name</var> * @param elementNS * Namespace of the element named <var>name</var> * @param name * Name of the element to be extracted * @return Element named <var>name</var>, <br> * or null if <var>message</var> contains zero or more than one * element named <var>name</var> */ public static Element extractElement(Element message, String elementNS, String name) { return extractElement(message, elementNS, name, true); } /** * Extracts the element named <var>name</var> with the namespace * <var>elementNS</var> that is a descentant of the element * <var>message</var>. This method returns null if the named elemens is not * a child of <var>message</var> or if <var>message</var> contains more than * one element named <var>name</var> * * @param message * Source Document that contains the element named * <var>name</var> * @param elementNS * Namespace of the element named <var>name</var> * @param name * Name of the element to be extracted * @return Element named <var>name</var>, <br> * or null if <var>message</var> contains zero or more than one * element named <var>name</var> */ public static Element extractElement(Element message, String elementNS, String name, boolean strict) { NodeList list = message.getElementsByTagNameNS(elementNS, name); if (list == null || list.getLength() == 0 || (strict && list.getLength() > 1)) return null; Element element = (Element) list.item(0); return element; } }