while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); String namespaceURI = startElement.getName().getNamespaceURI(); System.out.println("Namespace URI: " + namespaceURI); } }
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream); while (reader.hasNext()) { int event = reader.next(); if (event == XMLStreamConstants.START_ELEMENT) { String namespaceURI = reader.getNamespaceURI(); System.out.println("Namespace URI: " + namespaceURI); } }This example shows another way to use the getNamespaceURI method to obtain the namespace URI of the current XML element by using the XMLStreamReader interface directly. The method is used to get the namespace URI of the current element when parsing the XML document using a stream-based approach. Both examples demonstrate the use of the getNamespaceURI method to retrieve the namespace URI of the current XML element. The package library for the XMLStreamReader interface is javax.xml.stream.