Access elements from document with namespaces
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class Main {
public static void main(String[] argv) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
DocumentBuilder loader = factory.newDocumentBuilder();
Document document = loader.parse("sample.xml");
String docNS = "http://www.my-company.com";
Element order = document.getDocumentElement();
Attr date = order.getAttributeNodeNS(docNS, "date");
NodeList children = order.getElementsByTagNameNS(docNS, "item");
}
}
Related examples in the same category