Java tutorial
//package com.java2s; import java.io.ByteArrayInputStream; import java.io.CharArrayWriter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class Main { private final static Charset CHARSET_UTF8 = Charset.forName("UTF-8"); /** * * @param xmlContent * @param attributeName * @param matches * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException * @throws TransformerException */ public static String removeAttribute(String xmlContent, String attributeName, String... matches) throws SAXException, IOException, ParserConfigurationException, TransformerException { return removeAttribute(xmlContent, CHARSET_UTF8, attributeName, matches); } /** * * @param xmlContent * @param charset * @param attributeName * @param matches * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException * @throws TransformerException */ public static String removeAttribute(String xmlContent, Charset charset, String attributeName, String... matches) throws SAXException, IOException, ParserConfigurationException, TransformerException { Document document = parse(xmlContent, charset); Element element = document.getDocumentElement(); removeAttributeLoopElement(element, attributeName, matches); return write(document); } /** * * @param xmlContent * @param charset * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException */ private static Document parse(String xmlContent, Charset charset) throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(false); documentBuilderFactory.setValidating(false); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes(charset))); return document; } /** * * @param element * @param attributeName * @param matches */ private static void removeAttributeLoopElement(Element element, String attributeName, String... matches) { if (element.hasAttribute(attributeName)) { if (matches(element.getTagName(), matches)) { element.removeAttribute(attributeName); } } NodeList nodeList = element.getChildNodes(); int length = nodeList.getLength(); for (int seq = 0; seq < length; seq++) { Node node = nodeList.item(seq); if (node.getNodeType() == Node.ELEMENT_NODE) { if (element.hasAttribute(attributeName)) { if (matches(element.getTagName(), matches)) { removeAttributeLoopElement((Element) node, attributeName, matches); } } } } } /** * * @param document * @return * @throws TransformerException * @throws UnsupportedEncodingException */ private static String write(Document document) throws TransformerException, UnsupportedEncodingException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); Source source = new DOMSource(document); CharArrayWriter os = new CharArrayWriter(); Result result = new StreamResult(os); transformer.transform(source, result); return os.toString(); } /** * * @param tagName * @param matches * @return */ private static boolean matches(String tagName, String... matches) { if (matches == null || matches.length == 0) { return true; } for (String matche : matches) { if (tagName.matches(matche)) { return true; } } return false; } }