Example: RDDLStripper
import javax.xml.stream.*;
import java.net.*;
import java.io.*;
public class RDDLStripper {
public final static String RDDL_NS = "http://www.rddl.org/";
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("Usage: java RDDLStripper url" );
return;
}
try {
InputStream in;
try {
URL u = new URL(args[0]);
in = u.openStream();
}
catch (MalformedURLException ex) {
// Maybe it's a file name
in = new FileInputStream(args[0]);
}
XMLStreamReader parser
= XMLInputFactory.newInstance().createXMLStreamReader(in);
XMLStreamWriter serializer
= XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
while (true) {
int event = parser.next();
if (parser.isStartElement()) {
String namespaceURI = parser.getNamespaceURI();
if (!namespaceURI.equals(RDDL_NS)) {
serializer.writeStartElement(namespaceURI, parser.getLocalName());
// add attributes
for (int i = 0; i < parser.getAttributeCount(); i++) {
serializer.writeAttribute(
parser.getAttributeNamespace(i),
parser.getAttributeName(i),
parser.getAttributeValue(i)
);
}
// add namespace declarations
for (int i = 0; i < parser.getNamespaceCount(); i++) {
String uri = parser.getNamespaceURI(i);
if (!RDDL_NS.equals(uri)) {
serializer.writeNamespace(parser.getNamespacePrefix(i), uri);
}
}
}
}
else if (parser.isEndElement()) {
String namespaceURI = parser.getNamespaceURI();
if (!namespaceURI.equals(RDDL_NS)) {
serializer.writeEndElement();
}
}
else if (event == XMLStreamConstants.CHARACTERS
|| event == XMLStreamConstants.SPACE) {
serializer.writeCharacters(parser.getText());
}
else if (event == XMLStreamConstants.CDATA) {
serializer.writeCData(parser.getText());
}
else if (event == XMLStreamConstants.COMMENT) {
serializer.writeComment(parser.getText());
}
else if (event == XMLStreamConstants.DTD) {
serializer.writeDTD(parser.getText());
}
else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
serializer.writeEntityRef(parser.getLocalName());
}
else if (event == XMLStreamConstants.PROCESSING_INSTRUCTION) {
serializer.writeProcessingInstruction(parser.getPITarget(), parser.getPIData());
}
else if (event == XMLStreamConstants.END_DOCUMENT) {
serializer.flush();
break;
}
}
serializer.close();
parser.close();
}
catch (XMLStreamException ex) {
System.out.println(ex);
}
catch (IOException ex) {
System.out.println("IOException while parsing " + args[0]);
}
}
}