Skip to content

Instantly share code, notes, and snippets.

@djangofan
Created May 25, 2012 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djangofan/2789163 to your computer and use it in GitHub Desktop.
Save djangofan/2789163 to your computer and use it in GitHub Desktop.
A Groovy script that can strip XML attributes from a XML file
:: related to this Gist: https://gist.github.com/2790685
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashSet;
import java.util.Set;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
String attributeKey = "siteKey"
String xmlFileName = args[0]
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()
factory.setNamespaceAware(true)
Document doc = null
NodeList nodes = null
Set<String> ids = null
File xmlFile = new File( xmlFileName )
try {
doc = factory.newDocumentBuilder().parse( xmlFile )
XPathExpression expr = XPathFactory.newInstance().newXPath().compile("//@" + attributeKey)
ids = new HashSet<String>()
nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET)
} catch (SAXException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
} catch (ParserConfigurationException e) {
e.printStackTrace()
} catch (XPathExpressionException e) {
e.printStackTrace()
}
for (int i = 0; i < nodes.getLength(); i++) {
print(".") //progress indicator
println()
Element el = ((Attr) nodes.item(i)).getOwnerElement();
if ( el.hasAttribute( attributeKey ) ) el.removeAttribute( attributeKey );
}
Transformer transformer
StreamResult result = null
try {
transformer = TransformerFactory.newInstance().newTransformer()
transformer.setOutputProperty( OutputKeys.INDENT, "yes" )
result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc)
transformer.transform(source, result)
} catch (TransformerConfigurationException e) {
e.printStackTrace()
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace()
} catch (TransformerException e) {
e.printStackTrace()
}
String xmlString = result.getWriter().toString()
println(xmlString)
try {
BufferedWriter out = new BufferedWriter(new FileWriter( xmlFileName ))
out.write( xmlString )
out.close()
}
catch (IOException e)
{
System.out.println("Exception ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment