XMLStreamWriter writer = // create XMLStreamWriter writer.setPrefix("ns", "http://www.example.com/ns"); // set namespace prefix String prefix = writer.getPrefix("http://www.example.com/ns"); // get prefix for URI
XMLStreamWriter writer = // create XMLStreamWriter String prefix = writer.getPrefix("http://www.example.com/ns"); // get prefix for URI if (prefix == null) { writer.setPrefix("ns", "http://www.example.com/ns"); // set namespace prefix }This example retrieves the prefix for a URI using getPrefix() and if the prefix is not already set, sets it using the setPrefix() method. These examples are part of the Java package library javax.xml.stream.